Chapter 1. Getting Started(2)

来源:互联网 发布:烈火战神 完整源码 编辑:程序博客网 时间:2024/05/14 22:30

1.3. A Word About Comments

It is usually best to place a comment block above the code it explains.

 

Comment Pairs Do Not Nest(嵌套)

When commenting out a large section of a program, it can seem easiest to put a comment pair around a region that you want to omit temporarily. The trouble is that if that code already has a comment pair, then the newly inserted comment will terminate prematurely. A better way to temporarily ignore a section of code is to use your editor to insert single-line comment at the beginning of each line of code you want to ignore. That way, you need not worry about whether the code you are commenting out already contains a comment pair.

 

1.4. Control Structures

1.4.1. The while Statement

1.4.2. The for Statement

 

    #include <iostream>
    int main()
    {
        int sum = 0;
        // sum values from 1 up to 10 inclusive
        for (int val = 1; val <= 10; ++val)
            sum += val; // equivalent to sum = sum + val

        std::cout << "Sum of 1 to 10 inclusive is "
                  << sum << std::endl;
        return 0;
    }


the overall execution flow of this for is:

1、Create val and initialize it to 1.

2、Test whether val is less than or equal to 10.

3、If val is less than or equal to 10, execute the for body, which adds val to sum. If val is   not less than or equal to 10, then break out of the loop and continue execution with the first statement following the for body.

4、Increment val.

5、Repeat the test in step 2, continuing with the remaining steps as long as the condition is true.

 

When we exit the for loop, the variable val is no longer accessible. It is not possible to use val after this loop terminates. However, not all compilers enforce this requirement.

 

The most common kinds of errors a compiler will detect:

Syntax errors.(语法错误)

Type errors.

Declaration errors.

原创粉丝点击