C++ 学习笔记 (7)—— 程序流程结构

顺序结构、选择结构、循环结构

顺序结构:程序按顺序执行,不发生跳转

选择结构:依据条件是否满足,有选择的执行相应的功能

循环结构:依据条件是否满足,循环多次执行某代码

1.选择结构

1.1 if语句

作用:

执行满足条件的语句

if语句的三种形式:

单行格式if语句

多行格式if语句

多条件的if语句

 

eg:

单行if语句

#include <iostream>

using namespace std;

int main()

{

cout << ”选择结构练习:“<< endl;

//单行if语句

//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出分数

//1.用户输入分数

int score = 0;

cout <<”请输入一个分数:“<< endl;

cin >> score;

//2.打印输入的分数

cout << ”您输入的分数为:“<< score << endl;

//3.判断分数是否大于600,如果大于,那么输出

//注意事项:if条件后面不要加分号

if (score > 650)

{

cout << ”恭喜您考上一本大学!“ << endl;

}

system(”pause“);

return 0;

}

 

嵌套if语句

作用:

在if语句中,可以嵌套使用if语句,达到更精确的条件判断

 

1.2 三目运算

作用:

通过三目运算符实现简单的判断

语法:表达式1?表达式2:表达式3

解释:

如果表达式1的值为真,执行表达式2,并返回表达式2的结果 如果表达式1的值为假,执行表达式3,并返回表达式3的结果

eg:

#include <iostream>

using namespace std;

int main()

{

cout << ”三目运算:“ << endl;

int a = 89; int b = 0; a > b ? cout << ”nkihoa“<< endl ;

cout << ”你不好“ << endl;

system(”pause“);

return 0;

}

 

1.3 switch语句

作用:

执行多条分支语句

注意1:

swtch 语句中表达式类型只能是整型或者字符型

注意2:case里如果没有break,那么程序就会一直执行下去

总结:

与if语句相比,对于多条件判断时,是switch的结构清晰,执行效率高,缺点是switch不可以判断区间

语法:

switch(表达式) { case 结果1:执行语句;break; case 结果2:执行语句;break; .... default:执行语句;break; }

 

2.循环结构

2.1 while循环结构

作用:

满足条件,执行循环语句 语法:while(循环条件){循环语句}

解释:

只要循环条件的结果为真,就执行循环语句 注意事项:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环。

 

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout <<”while循环练习:“ << endl;

//在屏幕打印 0~10 int num = 0;

while (num <= 10)

{

cout << num << endl; num++;

}

system(”pause“);

return 0;

}

 

2.2 do....while循环语句

作用:

满足条件,执行循环语句 语法:do{循环语句}while{循环条件};

注意:与while区别在于do..while会先执行一次循环语句,再判断循环条件

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << ”进行do 。。while练习:“<< endl;

//1.在屏幕上输出0~9这10个数字

int num = 0; do

{

cout << num << endl; num++;

}while (num<10);

system(”pause“);

return 0;

}

 

2.3水仙花数案例练习

eg:

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

cout << ”进行水仙花数练习:“<< endl;

//利用do...while进行实现

//案例描述:水仙花数是指一个3位数,他的每个位上的数字的3次方之和等于它本身

//例如:1^3+5^3+3^3 = 153 //请利用do。。。while语句,求出所有三位数中的水仙花数 //所有的三位数:100~999

int num = 153;

do

{

int a = pow(num % 10, 3);

int b = pow(num / 10 % 10, 3);

int c = pow(num / 100, 3);

cout << a + b + c << endl;

if (a + b + c==num)

{

cout << num << endl;

}

num++;

}

while (num<1000);

system(”pause“);

return 0;

}

 

2.4 for循环

作用:

满足循环条件,执行循环语句

语法:

for(起始表达式;条件表达式;末尾循环体){循环语句;

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << ”for循环练习:“ << endl;

//打印0~9

for (int i = 0; i < 10; i++)

{

cout << i << endl;

}

system(”pause“);

return 0;

}

 

2.5 练习案例:

敲桌子 案例描述:

从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout <<”练习敲桌子案例:“ << endl;

//案例描述:从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出

for (int i = 0; i < 101; i++)

{ if ((i % 10 == 7) || (i / 10 % 10 == 7) || (i % 7) == 0)

{

cout <<”敲桌子: “ << i << endl;

}

else

{

cout << i << endl;

}

}

system(”pause“);

return 0;

}

 

2.6 练习案例:嵌套循环

作用:在循环体中再嵌套一层循环,解决一些实际问题 案例:例如我们想在屏幕中打印如下图片,就需要利用嵌套循环

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << ”嵌套循环练习:“ << endl;

//案例描述:打印10x10 的矩形

for (int i = 0; i < 10; i++)

{

for (int j = 0; j < 10; j++)

{

cout <<” * “;

}

cout << endl;

}

system("pause“);

return 0;

}

 

2.7 练习案例: 利用嵌套循环,实现九九乘法表

 

eg:

 

#include <iostream>

using namespace std;

int main()

{

cout <<"乘法口诀练习:" << endl;

for (int i = 1; i <=9 ; i++)

{

for (int j = 1; j <= i; j++)

{

cout << j << "*"<< i<< "="<< j *i <<"  ";

}

cout << endl;

}

system(&quot;pause&quot;);

return 0;

}

 

3.跳转语句

3.1 break 语句

作用:

用于跳出选择结构或者循环结构 break使用时机 出现在switch条件语句中,

作用是终止case并跳出switch 出现在循环语句 中,

作用是跳出当前的循环语句出现在嵌套循环中,跳出最近的内层循环语句

 

eg1:

#include <iostream>

using namespace std;

int main()

{

cout << "跳转语句break练习:" << endl;

//1.switch 中使用 break

//选择 1. 站着死 2.坐着死 3.躺着死

int select = 0;

cout << "请选择您的死法:"<<endl;

cout << "1. 站着死" << endl;

cout << "2. 坐着死"<< endl;

cout << "3. 躺着死"<< endl;

cin >> select;

switch (select)

{

case 1:

cout << "您选择的是 1 : 站着死。" << endl;

break;

case 2:

cout << "您选择的是 2 : 坐着死。" << endl;

break;

case 3: cout <<"您选择的是 3 : 躺着死。"<< endl;

break;

default:

cout <<"快选择死法"<< endl; break; }

 

//2.单个for循环

for (int i = 0; i < 10; i++) { if (i == 8)

{

break;

}

cout <<"i = "<< i << endl;

}

//嵌套for循环

for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++)

{

if (j == 2)

{

break;

}

cout << j; } cout << endl;

}

system("pause");

return 0;

}

 

3.2 continue语句

作用:

在循环语句中,跳过本次循环中余下的尚未执行的语句,继续执行下一次循环

注意:break会退出循环,而continue不会

 

eg:

for (int i = 0; i <= 100; i++)

{ if (i % 2 == 0)

{

continue; //执行到此,推出本次循环,不再向下执行

}

cout << i << endl;

}

posted @ 2022-04-10 23:34  雾枫  阅读(43)  评论(0编辑  收藏  举报