CH04_程序流程结构

程序流程结构

C/C++支持最基本的三种程序运行结构:

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

选择结构

if语句

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

单行格式if语句:if(条件){语句块}

示例:

#include <iostream>
using namespace std;

int main() {

	int score = 72;

	//判断条件是否满足
	if (score >= 60) {
		cout << "及格了" << endl;
	}

	system("pause");
	return 0;
}

多行格式if语句:if(条件){语句块}else{语句块}

示例:

#include <iostream>
using namespace std;

int main() {

	int score = 72;

	//判断条件是否满足
	if (score >= 60) {
		cout << "及格了" << endl;
	}
	else {
		cout << "继续努力" << endl;
	}

	system("pause");
	return 0;
}

多条件的if语句:if(条件){语句块}else if(条件){语句块}...else{语句块}

示例:

#include <iostream>
using namespace std;

int main() {

	int score = 0;

	cout << "请输入你的考试成绩:" << endl;
	cin >> score;

	//判断条件
	if (score >= 80) {
		cout << "优秀" << endl;
	}
	else if (score >= 70) {
		cout << "良好" << endl;
	}
	else if (score >= 60) {
		cout << "及格" << endl;
	}
	else {
		cout << "差" << endl;
	}

	system("pause");
	return 0;
}

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

示例:

#include <iostream>
using namespace std;

int main() {

	//学校组建校篮球队,面向全校招募队员,要求身高不低于1.75米

	char flag = 'n';
	double height = 0;

	cout << "你是本校学生吗?(y/n):" << endl;
	cin >> flag;
	cout << "你身高多少(cm):" << endl;
	cin >> height;

	if (flag == 'y') {

		if (height >= 175) {
			cout << "恭喜你,报名成功!" << endl;
		}
		else {
			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 age = 0;

	cout << "请输入你的年龄:"<<endl;
	cin >> age;

	cout << "你是:" << (age >= 18 ? "成年人" : "未成年人") << endl;

	system("pause");
	return 0;
}

switch语句

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

语法:

switch(表达式){
    case 值1:
        //语句块
        break;
    case 值2:
        //语句块
        break;
    case 值3:
        //语句块
        break;
    default:
        //语句块
        break;
}

示例:

#include <iostream>
using namespace std;

int main() {

	int mingCi = 0;

	cout << "请输入你的名次:" << endl;
	cin >> mingCi;

	switch (mingCi) {
	case 1:
		cout << "奖励一个女朋友" << endl;
		break;
	case 2:
		cout << "奖励一个笔记本电脑" << endl;
		break;
	case 3:
		cout << "奖励一个移动硬盘" << endl;
		break;
	default:
		cout << "什么奖励都没有" << endl;
		break;
	}

	system("pause");
	return 0;
}

循环结构

while循环

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

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

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

示例:

#include <iostream>
using namespace std;

int main() {

	//产生的随机数范围为:1-100
	//int num = rand() % 100 + 1;
 
	int count = 0;

	while (count < 10) {
		cout << "好好学习,天天向上!" << endl;
		count++;
	}
	
	system("pause");
	return 0;
}

do-while循环

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

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

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

示例:

#include <iostream>
using namespace std;

int main() {

	int score = 0;

	do {

		cout << "参加了一次考试,请输入分数:" << endl;
		cin >> score;

	} while (score<60);

	cout << "恭喜你,考试合格!" << endl;

	system("pause");
	return 0;
}

for循环

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

语法:for(初始部分;条件部分;迭代部分){循环语句}

示例:

#include <iostream>
using namespace std;


int main() {

	for (int i = 0; i < 10; i++) {
		cout << "hello world" << endl;
	}

	system("pause");
	return 0;
}

嵌套循环

作用:在循环体中再嵌套一层循环,解决一些实际问题

示例:

#include <iostream>
using namespace std;


int main() {

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

		for (int j = 1; j <= i; j++) {
			cout << j<<"*"<<i<<"="<<(j*i)<<"\t";
		}
		cout << endl;
		
	}

	system("pause");
	return 0;
}

跳转语句

break语句

作用:用于跳出选择结构或循环结构。

描述:

1.出现在switch条件语句中,作用是终止case并跳出switch。

2.出现在循环语句中,作用是跳出当前的循环语句。

3.出现在嵌套循环中,跳出最近的内存循环结构。

示例:

#include <iostream>
using namespace std;


int main() {
	int score = 0;

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

		cout << "请输入分数:" << endl;
		cin >> score;

		if (score < 0) {
			cout << "分数输入错误,终止录入!" << endl;
			break;
		}	
	}

	system("pause");
	return 0;
}

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语句时,会跳转到标及的位置。

示例:

#include <iostream>
using namespace std;


int main() {

	cout << "1" << endl;

	goto Flag;

	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;

	Flag:

	cout << "5" << endl;

	system("pause");
	return 0;
}
posted on 2024-06-11 20:30  熊猫_大侠  阅读(3)  评论(0编辑  收藏  举报