程序流程结构

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

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

选择结构

if语句

if语句的三种形式:

  • 单行if语句
  • 多行if语句
  • 多条件if语句

单行if语句:if(判断条件){执行代码}

#include <iostream>
using namespace std;

int main() {
    
    
	int score;

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

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		cout << "该分数已超过重点线,恭喜!" << endl;
	}

	system("pause");

	return 0;
}

多行if语句:if(判断条件){条件满足执行的代码}else{条件不满足执行的代码}

#include <iostream>
using namespace std;

int main() {
    
    
	int score;

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

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		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() {
    
    
	int score;

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

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		cout << "该分数已超过重点线,可喜可贺!" << endl;
	}
	else if (score >=500) 
	{
    
    
		cout << "该分数已超过一本线,恭喜!" << endl;
	}
	else
	{
    
    
		cout << "该分数未上线,还需继续努力!" << endl;
	}

	system("pause");

	return 0;
}

三目运算符

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

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

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

#include <iostream>
using namespace std;

int main() {
    
    
	int a = 10;
	int b = 20;
	int c;

	c = a > b ? a : b;  

	cout << c << endl;  // 结果为20

	a > b ? a : b = 100;  // 三目运算符返回的是变量,可以继续赋值

	cout << "a = " << a << endl;  // 结果为10
	cout << "b = " << b << endl;  // 结果为100

	system("pause");

	return 0;
}

switch语句

switch语句用于执行多条件分支语句;在判断的时候表达式只能是整型或字符型,不能写成一个数值区间。
相比于if语句,switch语句的结构清晰,执行效率要更高。
case里如果没有break语句,那么程序会一直向下执行。

语法:

switch(表达式)  
{
    
    
case 结果1: 
    执行代码;
	break;
	
case 结果2: 
    执行代码;
	break;
	
	...
	
default: 
    执行代码;
	break;
}
#include <iostream>
using namespace std;

int main() {
    
    
	int score;

	cout << "请输入电影评分:" << endl;
	cin >> score;

	switch (score)  // 表达式须为整型或字符型
	{
    
    
	case 10:
		cout << "非常经典的一部电影!" << endl;
		break;  // 如果没有break语句及时跳出,则程序会一直向下执行
	case 9:
		cout << "很不错的一部电影!" << endl;
		break;
	case 8:
		cout << "值得一看的一部电影!" << endl;
		break;
	default:
		cout << "这部电影不太行!" << endl;
		break;
	}

	system("pause");

	return 0;
}

循环结构

while循环语句

语法:while(循环条件){执行代码}

只要循环条件的结果为真,就会执行代码。

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 0;

	while (num < 10)  // 不能加分号
	{
    
    
		cout << num << endl;
		num++;
	}

	system("pause");

	return 0;
}

水仙花数:

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 100;
	int a;
	int b;
	int c;

	cout << "三位数的水仙花数有如下几个:" << endl;

	while (num >= 100 && num <= 999) {
    
    
		a = num % 10;  // 个位
		b = num / 10 % 10;  // 十位
		c = num / 100;  // 百位

		if (a * a * a + b * b * b + c * c * c == num) {
    
    
			cout << num << endl;
		}

		num++;
	}

	system("pause");

	return 0;
}

do…while循环语句

语法:do{执行代码}while(循环条件);

do…while会先执行一次循环代码,再判断循环条件。

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 0;

	do
	{
    
    
		cout << num << endl;
		num++;
	} while (num < 10);  // 需要加分号

	system("pause");

	return 0;
}

for循环语句

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

for(①起始表达式; ②条件表达式; ③末尾循环体){
    
    
	④执行代码
}
这段循环代码的执行顺序为:① -->-->-->-->-->-->-->...
for循环中的①②③可以放在for后面的括号内,也可以放在括号外,但括号内的两个分号不能少。
#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 0; i < 10; i++) {
    
    
		cout << i << endl;
	}

	system("pause");

	return 0;
}

下面这段代码跟上面这段代码的输出效果是一样的:

#include <iostream>
using namespace std;

int main() {
    
    
	int i = 0;

	for (; ; ) {
    
    
		if (i >= 10) {
    
    
			break;
		}

		cout << i << endl;
		
		i++;
	}

	system("pause");

	return 0;
}

打印乘法口诀表:

#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;
}

跳转语句

break

break语句用于跳出选择结构或循环结构。

break使用的时机:

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

continue

continue语句作用于循环中,目的是跳过本次循环,继续执行下一次循环。

#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 1; i < 100; i++) {
    
    
		if (i % 2 == 0) {
    
    
			continue;  // 可用于依据某些条件进行筛选
		}

		cout << i << endl;
	}

	system("pause");

	return 0;
}

goto

goto语句可以无条件跳转语句。

语法:goto 标记;

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

在程序中不建议使用goto语句,以免造成程序流程混乱。

#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 1; i < 10; i++) {
    
    
		if (i == 5) {
    
    
			goto FLAG;
		}

		cout << i << endl;
	}

	FLAG:
	cout << "程序结束!" << endl;

	system("pause");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_48158964/article/details/131481407
今日推荐