目录
  • 前言
  • 1 选择结构
    • 1.1 if语句(和C没啥不一样)
    • 1.2 三目运算符
    • 1.3 switch语句
  • 2 循环结构
    • 2.1 while 循环语句
    • 2.2 do…while循环语句
    • 2.3 for循环语句
    • 2.4 嵌套循环
  • 3 跳转语句
    • 3.1 break语句
    • 3.2 continue语句
    • 3.3 goto语句
  • 总结

    前言

    C/C++支持最基本的三种程序运行结构:顺序结构选择结构循环结构

    C++的程序流程结构你了解多少

    顺序结构就是顺着写代码,不想多说。

    1 选择结构

    1.1 if语句(和C没啥不一样)

    作用:执行满足条件的语句

    if语句的三种形式:

    单行格式if语句

    语法:

     if(条件){条件满足执行的语句}
    
    #include<iostream>
    using namespace std;
    int main()
    {
    	//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
    	//1.用户输入分数
    	int score = 0;
    	cout << "请输入一个分数:" << endl;
    	cin >> score;
    	cout << "您输入的分数为:" << score << endl;
    	//2.判断
    	if (score > 600)
    	{
    		cout << "恭喜您考上了一本大学" << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    多行格式if语句

    语法:

    if(条件){条件满足执行的语句}else{条件不满足执行的语句}
    
    #include<iostream>
    using namespace std;
    int main()
    {
    	//1.用户输入分数
    	int score = 0;
    	cout << "请输入一个分数:" << endl;
    	cin >> score;
    	cout << "您输入的分数为:" << score << endl;
    	//2.判断
    	if (score > 600)
    	{
    		cout << "恭喜您考上了一本大学" << endl;
    	}
    	else
    	{
    		cout << "您没有考上一本大学,请再接再厉" << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    多条件的if语句

    语法:

     if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句}... else{都不满足执行的语句}
    
    #include<iostream>
    using namespace std;
    int main()
    {
    	//1.用户输入分数
    	int score = 0;
    	cout << "请输入一个分数:" << endl;
    	cin >> score;
    	cout << "您输入的分数为:" << score << endl;
    	//2.判断
    	if (score > 600)
    	{
    		cout << "恭喜您考上了一本大学" << endl;
    	}
    	else if (score > 500)
    	{
    		cout << "恭喜您考上了二本大学" << endl;
    	}
    	else if (score > 400)
    	{
    		cout << "恭喜您考上了三本大学" << endl;
    	}
    	else
    	{
    		cout << "您没有考上一本大学,请再接再厉" << endl;
    	}
    	system("pause");
    	return 0;
    }
    

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

    #include<iostream>
    using namespace std;
    int main()
    {
    	//1.用户输入分数
    	int score = 0;
    	cout << "请输入一个分数:" << endl;
    	cin >> score;
    	cout << "您输入的分数为:" << score << endl;
    	//2.判断
    	if (score > 600)
    	{
    		if (score > 1000) { cout << "恭喜您考到了地球之外" << endl; }
    		if (score > 800) { cout << "恭喜您考到了清北" << endl; }
    		else { cout << "恭喜您考上了普通一本大学" << endl; }
    	}
    	else if (score > 500)
    	{
    		cout << "恭喜您考上了二本大学" << endl;
    	}
    	else if (score > 400)
    	{
    		cout << "恭喜您考上了三本大学" << endl;
    	}
    	else
    	{
    		cout << "您没有考上一本大学,请再接再厉" << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    C++的程序流程结构你了解多少

    #include<iostream>
    using namespace std;
    int main()
    {
    	int num1 = 0;
    	int num2 = 0;
    	int num3 = 0;
    	//用户输入
    	cout << "请输入小猪A的体重:" << endl;
    	cin >> num1;
    	cout << "请输入小猪B的体重:" << endl;
    	cin >> num2;
    	cout << "请输入小猪C的体重:" << endl;
    	cin >> num3;
    	cout << "小猪A的体重为:" << num1 << endl;
    	cout << "小猪B的体重为:" << num2 << endl;
    	cout << "小猪C的体重为:" << num3 << endl;
    	//判断
    	if (num1 > num2)     //A>B
    	{
    		if (num1 > num3) //A>C 
    		{
    			cout << "\n小猪A最重" << endl;
    		}
    		else             //C>A
    		{
    			cout << "\n小猪C最重" << endl;
    		}
    	}
    	else                 //B>A
    	{
    		if (num2 > num3) //B>C
    		{
    			cout << "\n小猪B最重" << endl;
    		}
    		else             //C>B
    		{
    			cout << "\n小猪C最重" << endl;
    		}
    	}
    	system("pause");
    	return 0;
    }
    

    1.2 三目运算符

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

    语法:

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

    解释:如果表达式1为真:执行表达式2,并返回表达式2的结果。

    如果表达式1为假:执行表达式3,并返回表达式3的结果。

    在C++中三目运算符如果后面的表达式是变量,则返回的是变量,并不是变量的值,也就是说可以继续对变量进行操作。

    #include<iostream>
    using namespace std;
    int main()
    {
    	int a = 10;
    	int b = 50;
    	int c = 0;
    	c = (a > b) ? a : b;
    	cout << "c = " << c << endl;
    	//在C++中三目运算符如果后面的表达式是变量,则返回的是变量,并不是变量的值,也就是说可以继续对变量进行操作
    	(a > b ? a : b) = 100;
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	system("pause");
    	return 0;
    }
    

    1.3 switch语句

    作用:执行多条件分支语句

    语法:

    switch(表达式){case 结果1 : 执行语句;break; case 结果2 : 执行语句;break; ... default : 执行语句;break;}break可以不加,不加break的时候程序会一直向下执行,即如果执行了结果2的执行语句,那么结果3、结果4...的执行语句都会被执行。switch(表达式)
    {case 结果1 : 执行语句;break;
     case 结果2 : 执行语句;break;
     ...
     default : 执行语句;break;
    }
    break可以不加,不加break的时候程序会一直向下执行,即如果执行了结果2的执行语句,那么结果3、结果4...的执行语句都会被执行。
    

    优点:结构清晰,执行的效率高;

    缺点:case所接的结果必须是整型或者字符型,且不能判断区间。

    #include<iostream>
    using namespace std;
    int main()
    {
    	int score = 0;
    	cout << "请给电影打分:" << endl;
    	cin >> score;
    	/* 5以下:烂片
    	   5 ~ 6:一般
    	   7 ~ 8:较好
    	   9~ 10:经典
    	*/
    	switch (score)
    	{
    	case 10:cout << "经典" << endl; break;
    	case 9:cout << "经典" << endl; break;
    	case 8:cout << "较好" << endl; break;
    	case 7:cout << "较好" << endl; break;
    	case 6:cout << "一般" << endl; break;
    	case 5:cout << "一般" << endl; break;
    	default:cout << "烂片" << endl; break;
    	}
    	system("pause");
    	return 0;
    }
    

    如果不加break的时候,执行效果如下:

    C++的程序流程结构你了解多少

    2 循环结构

    2.1 while 循环语句

    语法:

    while(循环条件){循环语句}
    

    解释:只要循环条件的结果为真,就执行循环语句。

    #include<iostream>
    using namespace std;
    int main()
    {
    	//在屏幕上打印0-9这10个数字
    	int num = 0;
    	while (num <= 9)
    	{
    		cout << num << endl;
    		num++;
    	}
    	system("pause");
    	return 0;
    

    while 循环案例:

    C++的程序流程结构你了解多少

    随机数的生成:C++产生随机数

    #include<iostream>
    #include<cstdlib> //可以不输入此行,iostream间接包含了这里的库头文件
    #include<ctime>
    using namespace std;
    int main()
    {	//srand(0)  不加此行或者使用它时,种子固定,所以产生随机数固定。
    	//种子控制产生的随机数,所以只需要产生随机种子就可了。
    	srand((int)time(0)); //利用当前系统时间产生随机种子,把0换成NULL也行。
    	int num = rand() % 100 + 1; //rand:伪随机数,rand()%100生成0-99
    	//cout << num << endl;
    	int val = 0;
    	while (1)
    	{
    		cin >> val;
    		if (val > num)
    		{
    			cout << "您猜的数大于此数" << endl;
    		}
    		else if (val < num)
    		{
    			cout << "您猜的数小于此数" << endl;
    		}
    		else 
    		{ cout << "恭喜您猜对了" << endl; break;
    		}
    	}
    	system("pause");
    	return 0;
    }
    

    2.2 do…while循环语句

    语法:

    do{循环语句} while(循环条件);
    

    与while的区别:do…while会先执行一次循环语句,再判断循环条件。

    #include<iostream>
    using namespace std;
    int main()
    {
    	//死循环,因为先执行了do里面的内容,所以while始终为真。
    	int num = 0;
    	do 
    	{
    		cout << num << endl;
    		num++;
    	} 
    	while (num);
    	system("pause");
    	return 0;
    }
    

    do…while 练习案例:

    C++的程序流程结构你了解多少

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
    	int num = 100;
    	int a = 0, b = 0, c = 0;
    	do
    	{
    		a = num / 100;       //百位
    		b = (num / 10 % 10); //十位
    		c = num % 10;	     //个位
    		if (pow(a, 3) + pow(b, 3) + pow(c, 3) == num)
    		{
    			cout << num << endl;
    		}
    		num++;
    	} while (num < 1000);
    	system("pause");
    	return 0;
    }
    

    2.3 for循环语句

    作用:满足循环条件,执行循环语句

    语法:

    for(起始表达式;条件表达式;末尾循环体){循环语句;}
    
    #include<iostream>
    using namespace std;
    int main()
    {
    	for (int i = 0; i < 10; i++)
    	{
    		cout << i << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    for中的表达式记得加;

    for 练习案例

    C++的程序流程结构你了解多少

    #include<iostream>
    using namespace std;
    int main()
    {	//个位有7,%10==7
    	//十位有7,/10==7
    	//7的倍数, %7==0
    	for (int i = 1; i <= 100; i++)
    	{
    		if ((i % 10 == 7) || (i / 10 == 7) || (i % 7 == 0)) 
    		{
    			cout << "敲桌子" << endl;
    		}
    		else { cout << i << endl; }
    	}
    	system("pause");
    	return 0;
    }
    

    2.4 嵌套循环

    用法:循环中再使用循环

    C++的程序流程结构你了解多少

    #include<iostream>
    using namespace std;
    int main()
    {	
    	for (int i = 0; i < 10; i++)
    	{
    		for (int j = 0; j < 10; j++)
    		{
    			cout << "* " ;
    		}
    		cout << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    嵌套循环案例

    C++的程序流程结构你了解多少

    #include<iostream>
    using namespace std;
    int main()
    {
    	for (int i = 1; i < 10; i++)
    	{
    		for (int j = 1; j <=i ; j++)
    		{
    			cout << j << "*" << i << "="<< j * i <<	"\t";
    		}
    		cout << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    3 跳转语句

    3.1 break语句

    作用:跳出选择结构(实际上只有switch语句用得到)或者循环结构。

    具体而言也就三种:

    • 出现在switch语句:终止case、跳出switch
    • 出现在循环语句:跳出当前的循环
    • 出现在嵌套循环中:跳出最近的内层循环(此break所属循环)。

    前两个很简单,只看第三个情况,对乘法口诀表的代码稍微进行修改即可:

    #include<iostream>
    using namespace std;
    int main()
    {
    	for (int i = 1; i < 10; i++)
    	{
    		for (int j = 1; j < 10; j++)
    		{
    			cout << j << "×" << i << "=" << j * i << "\t";
    			if (i == j)
    			{
    				break;
    			}
    		}
    		cout << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    3.2 continue语句

    作用:跳过本次循环中余下的执行语句,直接执行下一次循环。

    #include<iostream>
    using namespace std;
    int main()
    {
    	for (int i = 1; i <= 10; i++)
    	{
    		if (i == 3 || i == 7) { continue; }
    		cout << i << endl;
    	}
    	system("pause");
    	return 0;
    }
    

    3.3 goto语句

    作用:可以无条件跳转语句

    语法:

    goto 标记;

    解释;如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

    #include<iostream>
    using namespace std;
    int main()
    {
    	cout << "1.xxxx" << endl;
    	cout << "2.xxxx" << endl;
    	goto Flag;
    	cout << "3.xxxx" << endl;
    Flag: cout << "4.xxxx" << endl;
    	cout << "5.xxxx" << endl;
    	system("pause");
    	return 0;
    }
    

    一般不建议使用goto,因为标记太多,会导致代码及其混乱。

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!     

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。