Chapter 1. Getting Started(2)

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值