c++primer第五章循环和关系表达式学习笔记

for循环

简单for循环

#include <iostream>
using namespace std;
int main()
{ // 5.1
  int i;
  for (i = 0; i < 5; i++)
    cout << "C++ knows loops. \n";
  cout << "C++ knows when to stop.\n";
  return 0;
}

for循环组成部分

#include <iostream>
using namespace std;
int main()
{
    //5.2
    cout << "Enter the starting countdown value: ";
    int limit;
    cin >> limit;
    int i;
    for (i = limit; i; i--)
        cout << "i = " << i << "\n";
    cout << "Done now that i = " << i << "\n";
    return 0;
}

 i = 0 自动类型转换成bool类型的false

表达式和语句

x = y = z = 0; 赋值操作从右往左

age = 100 是表达式

age = 100; 是语句

 for (expression, expression, expression)

      statement;

#include <iostream>
using namespace std;
int main()
{
  // 5.3
  int x;
  cout << "The expression x = 100 has the value ";
  cout << (x = 100) << endl;
  cout << "Now x = " << x << endl;
  cout << "The expression x < 3 has the  value ";
  cout << (x < 3) << endl;
  cout << "The expression x > 3 has the  value ";
  cout << (x > 3) << endl;
  cout.setf(ios_base::boolalpha);
  cout << "The expression x < 3 has the  value ";
  cout << (x < 3) << endl;
  cout << "The expression x > 3 has the  value ";
  cout << (x > 3) << endl;
  return 0;
}

for循环计算阶乘

#include <iostream>
using namespace std;
int main()
{
  // 5.4
  const int ArSize = 16;
  double factorials[ArSize];
  factorials[1] = factorials[0] = 1.0;
  int i;
  for (i = 2; i < ArSize; i++)
    factorials[i] = i * factorials[i - 1];
  for (i = 0; i < ArSize; i++)
    cout << i << "! = " << factorials[i] << endl;
  return 0;
}

ArSize 修改就可以快速获得所有的常数的修改

修改步长

#include <iostream>
using namespace std;
int main()
{
  // 5.5修改步长
  cout << "Enter an integer: ";
  int by;
  cin >> by;
  cout << "Counting by " << by << "s: \n";
  for (int i = 0; i < 100; i = i + by)
    cout << i << endl;
  return 0;
}

使用for循环访问字符串

#include <iostream>
using namespace std;
int main()
{
  // 使用for循环 访问字符串
  cout << "Enter a word: ";
  string word;
  cin >> word;

  for (int i = word.size() - 1; i >= 0; i--)
    cout << word[i];
  cout << "\nBye.\n";
  return 0;
}

递增操作符和递减操作符

#include <iostream>
using namespace std;
int main()
{
  int a = 20;
  int b = 20;

  cout << "a = " << a << "; b = " << b << "\n";
  cout << "a++ = " << a++ << "; ++b = " << ++b << endl;
  cout << "a = " << a << "; b = " << b << "\n";
  return 0;
}

 a++ 先给出表达式结果然后再加1; ++b 先加1然后用新的值来给出表达式结果

   副作用(side effect)和顺序点(sequence point)

    int guests = 9;

    while (guests++ < 10)

        printf("%d \n", guests);

    guests 和10 比较之后加1;对于类而言,前缀版本的效率更高

递增递减操作符和指针

#include <iostream>
using namespace std;
int main()
{
  double arr[5] = {21.1, 32.8, 23.4, 45.2, 37.4};
  double *pt = arr;

  ++pt;
  cout << pt << endl;
  cout << *pt << endl;
  // ++*pt;  //int 类型的++
  *pt++;
  cout << pt << endl;
  cout << *pt << endl;

  return 0;
}

组合赋值操作符

i = i + by  等价于     i += by

    int k = 5;

    k += 3;

    int *pa = new int[10];

    pa [4] = 12;

    pa [4] +=6; 

    *(pa + 4) += 7;

    pa += 2;

    a += b  等价于   a = a + b

    a -= b  等价于   a = a - b

    a *= b 等价于    a = a * b

    a /= b  等价于   a = a / b

    a %= b  等价于  a = a % b

复合语句

#include <iostream>
using namespace std;
int main()
{
  // program5.8
  cout << "The Amazing Account will sum and average ";
  cout << "five numbers for you. \n";
  cout << "Please enter five values: \n";
  double number;
  double sum = 0.0;
  for (int i = 1; i <= 5; i++)
  {
    cout << "Value " << i << ": ";
    cin >> number;
    sum += number;
  }
  cout << "Five exquisite choices indeed! ";
  cout << "They sum to " << sum << endl;
  cout << "and average to " << sum / 5 << ".\n";
  cout << "The Amazing Account bids you adieu!\n";
  return 0;
}

 

外部语句定义的变量在内部是被定义,反之是没有被定义的

int x = 20;
    {
        cout << x << endl;
        int x = 100;
        cout << x << endl;
    }
    cout << x << endl;
    return 0;

 逗号操作符

将两个表达式放在原来只能放一个表达式的地方

#include <iostream>
using namespace std;
int main()
{
  cout << "Enter a word: ";
  string word;
  cin >> word;

  char temp; // 在外部声明变量可以提高速度
  int i, j;
  for (j = 0, i = word.size() - 1; j < i; --i, ++j)
  {
    temp = word[i];
    word[i] = word[j];
    word[j] = temp;
  }
  cout << word << "\nDone\n";
  return 0;
}

i = 20, j = 2 * i   逗号表达式的值是第二部分, 并且逗号表达式的优先级是最低的

关系表达式

 可能犯的错误

#include <iostream>
using namespace std;
int main()
{
    //== 等于, != 不等于 5.10
    int quizscores[10] =
        {20, 20, 20, 20, 20, 19, 20, 18, 20, 20};

    cout << "Doing it right: \ n";
    int i;
    for (i = 0; quizscores[i] == 20; i++)
        cout << "quiz " << i << " is a 20 \n";

    cout << "Doing it dangerously wrong: \n";
    for (i = 0; quizscores[i] = 20; i++)
        cout << "quiz " << i << " is a 20 \n";

    return 0;
}

 

C-风格字符串的比较

    word == "mate" 数组名是数组的地址,所以左边的判断表达式表示的是是否存储在相同的地址上。

    strcmp() 可以判断两个字符串是否相等

 不能关系操作符比较字符串,但是字符是整形所以 是可以比较的

  for (char ch = 'a'; ch < 'z'; ch++)
        cout << ch;
    return 0;

 

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char word[5] = "?ate";

    for (char ch = 'a'; strcmp(word, "mate"); ch++)
    {
        cout << word << endl;
        word[0] = ch;
    }
    cout << "After loop ends, word is " << word << endl;
    return 0;
}

 

strcmp(str1, str2) 如果这两个是不同的则输出是true,  同时还可以比较顺序

比较 string类字符串

#include <iostream>
#include <string>
using namespace std;
int main()
{
    // 5.12  比较 string类字符串
    string word = "?ate";

    for (char ch = 'a'; word != "mate"; ch++)
    {
        cout << word << endl;
        word[0] = ch;
    }
    cout << "After loop ends, word is " << word << endl;
    // C++ 使用String类型不用提前规定大小
}

while 循环

while 循环

    while(test-condition)

        body

#include <iostream>
const int ArSize = 20;
using namespace std;
int main()
{

    // 5.12
    char name[ArSize];
    cout << "Your first name: please: ";
    cin >> name;
    cout << "Here is your name: verticalized and ASCIIized: \n";
    int i = 0;
    while (name[i] != '\0')
    {
        cout << name[i] << ": " << int(name[i]) << endl;
        i++;
    }
    return 0;
}

for 循环可以知道所有的信息, while 循环无法预先知道所执行的次数

while (for) 后面加循环,则变成了空循环

等待一段时, 延时循环

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    cout << "Enter the delay time, in second: ";
    float secs;
    cin >> secs;
    clock_t delay = secs * CLOCKS_PER_SEC;
    cout << "starting \a \n";
    clock_t start = clock();
    while (clock() - start < delay)
        ;
    cout << "done \a\n";
    return 0;
}

 

do while 循环

 do

       body

    while (test-expression)

 

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter numbers in the range 1-10 to find ";
    cout << "My favorite number\n";
    do
    {
        cin >> n;
    } while (n != 7);
    cout << "Yes, 7 is my favorite number. \n";
    return 0;
}

 循环和文本输入

cin输入

#include <iostream>
using namespace std;
int main()
{
    // 哨兵字符,作为停止标记
    char ch;
    int count = 0;
    cout << "Enter characters; Enter # to quit: \n";
    cin >> ch;
    while (ch != '#')
    {
        cout << ch;
        ++count;
        cin >> ch;
    }
    cout << endl
         << count << " characters read \n";
    return 0;
}

输出没有空格

cin.get()输入

修改代码

#include <iostream>
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout << "Enter characters; Enter # to quit: \n";
    cin.get(ch);
    while (ch != '#')
    {
        cout << ch;
        ++count;
        cin.get(ch);
    }
    cout << endl
         << count << " characters read \n";
    return 0;
}

cin.get()的两个使用方法

 文件尾条件

#include <iostream>
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cin.get(ch);
    while (cin.fail() == false)
    {
        cout << ch;
        ++count;
        cin.get(ch);
    }
    cout << endl
         << count << "characters read \n";
    return 0;
}

 嵌套和二维数组

二维数组每一个元素本身就是一个数组。

#include <iostream>
using namespace std;
const int Cities = 5;
const int Years = 4;
int main()

{
    // 二维数组初始化,逗号隔开
    const char *cities[Cities] = // 字符串类型指针
                                 // char cities[Cities][25] =
                                 //     const string cities[Cities] =
        {
            "Gribble City",
            "Gribble town",
            "New Gribble",
            "San Gribble",
            "Gribble Vista"};
    int maxtemps[Years][Cities] =
        {
            {54, 53, 86, 100, 104},
            {95, 97, 90, 106, 102},
            {96, 100, 940, 107, 105},
            {97, 102, 89, 108, 104}};
    cout << "Maximum temperatures for 2002-2005 \n\n";
    for (int city = 0; city < Cities; ++city)
    {
        cout << cities[city] << ":\t";
        for (int year = 0; year < Years; ++year)
            cout << maxtemps[year][city] << "\t";
        cout << endl;
    }
    return 0;
}

  • 17
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值