第四章 程序流程结构

4.程序流程结构

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

4.1 选择结构

4.1.1 if语句

1.单行if语句:if(条件){条件满足时执行的语句}

2.多行if语句

if(条件){}
else if(条件){}
else{}

3.嵌套if语句

if(条件)
{
	if(条件){}
	else{}
}
else{}
4.1.2 三目运算符

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

2.解释:若表达式1的值为真 —> 执行表达式2,并返回表达式2的值

​ 若表达式1的值为假 —> 执行表达式3,并返回表达式3的值

3.注意:在C++中三目运算符若返回的是变量,可以继续赋值

int a,b,c;
a = 10, b = 20, c = 0;
c = (a > b ? a : b);  // 表示若a>b,则c赋予a的值,否则赋予b的值
(a > b ? a : b) = 30; // 表示若a>b,则a赋值30,否则b赋值30
4.1.3 switch语句

1.语法:

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

2.优缺点:结构清晰且执行效率高,但判断的表达式只能是整型或字符型常量

3.实例:

	int score = 0;
	cout << "请输入您的成绩:";
	cin >> score;
	switch (score)
	{
	case 60:
		cout << "不及格" << endl; break;
	case 70:
		cout << "及格" << endl;
		cout << "再接再厉 " << endl;
		break;
	case 80:
		cout << "良好" << endl; break;
	case 90:
		cout << "优秀" << endl; break;
	default:
		break;
	}

由上述实例可以看出:①switch()内部只能是整型或字符型

​ ②case后的缩进处可以放置不止一个表达式


4.2 循环结构

4.2.1 while语句

1.语法:while(条件){}

2.解释:当条件为真时,执行{ }中的语句

3.实例:

/*使用while语句实现“猜数字”游戏,1~100之间由玩家输入,只能回复大小*/

#include<iostream>	
using namespace std;

int main() {
	int num_set = rand() % 100 + 1;
	int num_input = 0;
	int num_time = 1;

	cout << "0~100,请输入你所猜的数字:";
	cin >> num_input;
	while (num_input != num_set)
	{
		if (num_input < num_set)
			cout << "猜小了" << endl;
		else
			cout << "猜大了" << endl;
		cout << "重新输入你所猜的数字:";
		cin >> num_input;
		num_time++;
	}
	cout << "猜对了,你一共猜了 " << num_time << " 次" << endl;
	system("pause");
	return 0;
}
4.2.2 do while语句

1.语法:

do
{
	执行语句
}while(条件)

2.解释:先执行do中的语句,再去判断while中的条件,若为真则进入下一次循环

3.实例:

/*              使用do while语句求出所有三位数中的水仙花数。
水仙花数即为:该数的每一位的三次幂之和等于该数本身,例如153 = 1^3 + 5^3 + 3^3 */

#include<iostream>
using namespace std;

int main() {
	int num = 100;
	int num_time = 0;
	int bw, sw, gw;
	do
	{
		bw = num / 100;
		sw = (num % 100) / 10;
		gw = num % 10;
		if ((bw*bw*bw + sw*sw*sw + gw*gw*gw) == num)
		{
			printf("第%d个水仙花数为:%d\n", ++num_time, num);
		}
	} while (++num <= 999);

	system("pause");
	return 0;
}

注意:在C++中的幂运算只能连乘或使用<math.h>里面的pow函数,不能使用^(^在C++中表示异或运算

4.2.3 for语句

1.语法:for(启示表达式;终止表达式;末尾循环体){循环语句}

2.实例:

/*1~100,逢含7或本身是7的倍数则跳过,打印其他数字*/

#include<iostream>
using namespace std;

int main() {
	int gw, sw, bw;
	for (int num = 1; num <= 100; num++)
	{
		bw = num / 100;
		sw = (num % 100) / 10;
		gw = num % 10;
		if ((gw == 7) || (sw == 7) || (bw == 7) || ((num % 7) == 0))
			cout << "敲桌子" << endl;
		else
			cout << num << endl;
	}


	system("pause");
	return 0;
}
4.2.4 嵌套循环

1.实例:

/*使用嵌套循环 绘制乘法口诀表*/

#include<iostream>
using namespace std;

int main() {
	for (int i=1 ; i<=9 ; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			printf("%d × %d = %d\t", j, i, i * j);
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

4.3 跳转语句

4.3.1 break语句

跳出本层循环结构,或跳出本次switch-case选择结构

4.3.2 continue语句

跳出本次循环执行下一次循环

4.3.3 goto语句

1.语法:

xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx
goto FLAG;
xxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx
FLAG:
xxxxxxxxxxxxxxxx

2.作用:强制跳转代码位置,不建议使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值