c++primer的学习|section1.4. 控制结构

1.4. 控制结构
少数程序用顺序结构即可编写,实际上,程序设计语言提供了控制结构编写更为复杂的语句。

while语句提供了迭代执行功能,编写计算1-10的程序:
#include <iostream>

int main(){
        int sum=0;
        int sal=1;
        while(sal<=10){

            sum=+sal;
            ++sal;

}

std::<<sum<<std::endl;
return 0;
}

由于while语句频频使用sal变量,为了简化语句,提供了for语句:
#include <iostream>

int main(){
    int sum=0;int sal=1;

    for(sal=1;sal<=10;sal++){
        sum=+sal;
}

std::<<sum<<std::endl;
return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41245033/article/details/80288858
今日推荐