lesson_7用for循环语句例析.ppt

例:请计算并输出1~9这9个数的平方。 coutx*x; 例程: #include iostream using namespace std; int main(){ cout1*1endl; cout2*2endl; cout3*3endl; cout4*4endl; cout5*5endl; cout6*6endl; cout7*7endl; cout8*8endl; cout9*9endl; return 0; } 用for循环语句解决该问题 #include iostream using namespace std; int main(){ for(int i=1;i=9;i++){ couti*iendl; } return 0; } for循环语句 循环的概念 计算机在执行一个程序模块时,不是按照语句的排列顺序进行的,而是按照一定的规律和规则在一定的条件下重复执行模块内部的一系列语句,那么我们可以把这种结构看作是循环结构。这种结构的程序设计可以称为循环程序设计。 C++语言的循环语句有三种:for语句;while语句 ;do~while语句。 for循环语句格式 for ([表达式1];[表达式2];[表达式3]){ 循环体 } 解释:for语句的功能是表达式2返回为真时,重复执行循环体。即先执

文档评论(0)

1亿VIP精品文档

相关文档