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

  • 顺序结构:程序按顺序执行,不发生跳转
  • 选择结构:依据条件是否满足,有选择的执行相应功能
  • 循环结构:依据条件是否满足,循环多次执行某段代码

选择结构


执行满足条件的语句

关于if,前面已经提过了:if 语句

三目运算符

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

语法:表达式1 ? 表达式2 : 表达式3
语法解释:当表达式1的值为True时,执行表达式2,否则,执行表达式3

示例:

#include <iostream>

using namespace std;

int main()
{
	int a = 10, b = 20, c = 30;
	int result = a == 10 ? b : c;
	cout << result << "\n"; // 20
	
	a = 20;
	result = a == 10 ? b : c;
	cout << result << "\n"; // 30
	
	//C++中三目运算符返回的是变量,可以继续赋值
	(a > b ? a : b) = 1000;
	cout << "a=" << a << "\tb=" << b; // b=1000

	return 0;	
}

switch语句

执行多条件分支语句,相当于:if - else if - else if...else

语法:

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

其中:

  • case:表达式接收到的一个具体的值,当表达式的值为这个值时,将会执行其语句
  • default:相当于 else,当case都无法接收这个值时,将会执行其语句。

关于 break
break 是循环语句或switch语句中专属的,当运行到这个语句时,将会跳出循环或switch的条件分支

为什么需要 break?、
switch默认是顺序执行的,什么意思呢?就是从上往下执行,如果不使用break就会:

#include <iostream>

using namespace std;

int main()
{
	int input, count = 0;
	cout << "ÇëÊäÈë:";
	cin >> input;
	switch (input) {
		case 1:
			//TODO
			count++;
		case 2:
			//TODO
			count++;
			cout << "2" <<"\n";
		case 3:
			//TODO
			count++;
			cout << "3" <<"\n";
		default:
			//TODO
			count++;
			break;
	}
	cout << "count=" << count << "\n";

	return 0;
}

输出:

请输入:3
3
count=2

使用break的话,可以在每次case选项时,保证这个选项会跳出分支。

注意:
switch语句中表达式类型只能是整型或者字符型

循环结构


while循环语句

当满足条件时,将会重复执行语句

语法:

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

其执行流程如下:

使用示例:

#include <iostream>

using namespace std;

int main()
{
	int count = 1;
	while(count <= 100){
		cout << "count=" << count << "\n";
		count++;
	}

	return 0;
}

注意:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环,比如:

#include <iostream>

using namespace std;

int main()
{
	int count = 1;
	while(count <= 100){
		cout << "count=" << count << "\n";
	}

	return 0;
}

猜数字小案例:

案例描述: 系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并且退出游戏。

#include <iostream>
#include <random>

using namespace std;

int main()
{
	random_device rd;
	mt19937 gen(rd());
	
	// 定义分布范围
	uniform_int_distribution<> dis(1, 100);
	
	// 生成随机数
	int randomNumber = dis(gen);
	
	int input;
	cout << "请输入你要猜的数字: ";
	cin >> input;
	while(input != randomNumber) {
		string r = input > randomNumber ? "数字过大" : "数字过小";
		cout << r << "\n";
		cout << "请输入你要猜的数字: ";
		cin >> input;
	} 
	cout << "恭喜你,猜对了!" << "\n";
	
	return 0;
}

do...while循环语句

无论是否满足条件,都会执行一次do。
与while的区别在于==do...while会先执行一次循环语句,再判断循环条件==

语法:

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

使用示例:

#include <iostream>

using namespace std;

int main()
{
//	int num = 0;
	int num = 10;
	do
	{
		cout << num << "\n";
		num++;
	} while(num < 10);
	
	return 0;
}

若num=10,将会输出:10

练习案例:水仙花数

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

例如:1^3 + 5^3+ 3^3 = 153

请利用do...while语句,求出所有3位数中的水仙花数

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int num = 100;
	do{
		int first = num / 100;
		int second = (num / 10) % 10;
		int third = num % 10;
		// 计算各位数字的立方和
		int sum = pow(first, 3) + pow(second, 3) + pow(third, 3);
		
		if (sum == num)
		{
			cout << "水仙花数:" << num << "\n";
		}
		num++;
	}while(num < 1000);
}

for循环语句

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

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

示例:

int main() {  
​  
    for (int i = 0; i < 10; i++)  
    {  
        cout << i << endl;  
    }  
      
    system("pause");  
​  
    return 0;  
}

跳转语句


break语句

break之前在 switch语句 提到过,其作用为 用于跳出==选择结构==或者==循环结构==

break使用的时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch
  • 出现在循环语句中,作用是跳出当前的循环语句
  • 出现在嵌套循环中,跳出最近的内层循环语句

示例1:

#include <iostream>

using namespace std;

int main()
{
	cout << "请选择你挑战副本的难度: " << "\n";
	cout << "1、普通" << "\n";
	cout << "2、中等" << "\n";
	cout << "3、困难" << "\n";
	
	int num = 0;
	
	cin >> num;
	
	switch (num) {
		case 1:
			cout << "普通" << "\n";
			break;
		case 2:
			cout << "中等" << "\n";
			break;
		default:
			cout << "困难" << "\n";
			break;
	}
	
	return 0;
}

示例2:

int main() {  
    //2、在循环语句中用break  
    for (int i = 0; i < 10; i++)  
    {  
        if (i == 5)  
        {  
            break; //跳出循环语句  
        }  
        cout << i << endl;  
    }  
​  
    system("pause");  
​  
    return 0;  
}

示例3:

int main() {  
    //在嵌套循环语句中使用break,退出内层循环  
    for (int i = 0; i < 10; i++)  
    {  
        for (int j = 0; j < 10; j++)  
        {  
            if (j == 5)  
            {  
                break;  
            }  
            cout << "*" << " ";  
        }  
        cout << endl;  
    }  
      
    system("pause");  
​  
    return 0;  
}

continue语句

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

示例:

求出奇数

int main() {

	for (int i = 0; i < 100; i++)
	{
		if (i % 2 == 0)
		{
			continue;
		}
		cout << i << endl;
	}
	
	system("pause");

	return 0;
}

注意:continue并没有使整个循环终止,而break会跳出循环

goto语句

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

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

语法: goto 标记

示例:

#include <iostream>

using namespace std;

int main()
{
	cout << "1" << "\n";
	
	goto FLAG;
	
	cout << "2" << "\n";
	cout << "3" << "\n";
	cout << "4" << "\n";
	
	FLAG:
	cout << "5" << "\n";
		
	return 0;
}

输出:

1
5