这一集视频我们主要来看一下 C++ Primer 1.4 节。

主要是对几个常用的控制流的语句进行说明,包括,

  • while 循环
  • for 循环
  • 读取输入
  • if 语句

我们这里的讲解主要是对书上示例的代码进行相应的解析,对于大家容易理解的知识点,就不去花费多余的时间来说明。删繁就简,同时,不漏疑难点。

Part1 while 循环

首先是 while 语句,核心是理解这样一句话,

while 语句反复执行一段代码,直至给定条件为假为止。

直接看书中的示例代码,

 1#include <iostream>
 2int main()
 3{
 4    int sum = 0, val = 1;
 5    // keep executing the while as long as val is less than or equal to 10
 6    while (val <= 10)
 7    {
 8        sum += val; // assigns sum + val to sum
 9        ++val;      // add 1 to val
10    }
11    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
12    return 0;
13}

Part2 for 循环

然后是 for 语句,我们更加常用的一种循环语句,

比如,书上的例子是从 1 加到 10,

 1#include <iostream>
 2int main()
 3{
 4    int sum = 0;
 5    // sum values from1 through 10 inclusive
 6    for (int val = 1; val <= 10; ++val)
 7        sum += val; // equivalent to sum = sum + val
 8    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
 9    return 0;
10}

for 循环后面如果不加花括号,那么,for 循环中只会执行一条语句,

 1#include <iostream>
 2int main() {
 3    int sum = 0;
 4    // sum values from1 through 10 inclusive
 5    for (int val = 1; val <= 10; ++val) 
 6        sum += val;  // equivalent to sum = sum + val
 7        sum += 100; 
 8    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
 9    return 0;
10}
11// output: 155

我们再来看加了花括号的效果,

 1#include <iostream>
 2int main() {
 3    int sum = 0;
 4    // sum values from1 through 10 inclusive
 5    for (int val = 1; val <= 10; ++val) {
 6        sum += val;  // equivalent to sum = sum + val
 7        sum += 100; 
 8    }
 9    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
10    return 0;
11}
12// output: 1055

Part3 读取输入

读取输入数据。

 1#include <iostream>
 2int main()
 3{
 4    int sum = 0, value = 0;
 5    // read until end-of-file, calculating a running total of all values read
 6    while (std::cin >> value)
 7        sum += value; // equivalent to sum = sum + value
 8    std::cout << "Sum is: " << sum << std::endl;
 9    return 0;
10}

从键盘输入文件结束符(EOF, end of file)这里,书上说的是在 Windows 下,输入文件结束符的方法是敲 Ctrl + Z,然后按回车键,但是在上面这个程序中无法体现,因为我们只要输入的不是一个 int 值,循环就会结束,比如,我们输入 Ctrl + D 然后回车也是可以的。所以,我们可以单独写一个程序来验证一下这个文件结束符,

 1// 测试一下 Windows 下 Ctrl + Z 然后回车是否是 EOF
 2#include <cstdio>
 3#include <iostream>
 4
 5int main()
 6{
 7    int x;
 8    if ((x = std::cin.get()) == EOF)
 9    {
10        std::cout << "Here is an EOF." << std::endl;
11    }
12    return 0;
13}

同时,这一小节中,书上还介绍了编写程序时会导致的一些编译错误,这里具体演示一下,

  • 语法错误
  • 类型错误
  • 声明错误

首先是语法错误,

然后是类型错误,比如,

1int a = "this is a string";

然后是声明错误,

1#include <iostream>
2int main()
3{
4    int v1 = 0, v2 = 0;
5    std::cin >> v >> v2; // error: uses "v" not "v1"
6    // error: cout not defined; should be std::cout
7    cout << v1 + v2 << std::endl;
8    return 0;
9}

Part4 if

 1#include <iostream>
 2int main()
 3{
 4    // currVal is the number we’re counting; we’ll read new values into val
 5    int currVal = 0, val = 0;
 6    // read first number and ensure that we have data to process
 7    if (std::cin >> currVal)
 8    {
 9        int cnt = 1; // store the count for the current value we’re processing
10        while (std::cin >> val)
11        {                       // read the remaining numbers
12            if (val == currVal) // if the values are the same
13                ++cnt;          // add 1 to cnt
14            else
15            { // otherwise, print the count for the previous value
16                std::cout << currVal << " occurs " << cnt << " times" << std::endl;
17                currVal = val; // remember the new value
18                cnt = 1;       // reset the counter
19            }
20        } // while loop ends here
21        // remember to print the count for the last value in the file
22        std::cout << currVal << " occurs " << cnt << " times" << std::endl;
23    } // outermost if statement ends here
24    return 0;
25}
26// input: 42 42 42 42 42 55 55 62 100 100 100