4_C++_程序流程结构

1、三种程序结构
顺序结构
选择结构(分支)
循环结构

2、选择结构
单行格式:if ( 条件 ) { 条件执行语句 }
注意:if条件后面,语句块后面,不要加分号
多行格式:if ( 条件 ) { 条件执行语句 } else { 条件执行语句 }
跟C语言差不多
多条件语句:if ( 条件1 ) { 条件执行语句1 } else if ( 条件2 ) { 条件执行语句2 } …else { 条件执行语句n+1 }
条件结构的嵌套:在条件执行语句里面在放入if语句,作用是更加精准执行任务

#include <iostream>
using namespace std;


int main() 
{
	int A, B, C;
	cout << "Please input 3 numbers A,B and C." << endl;
	cin >> A;
	cin >> B;
	cin >> C;
	cout << "The A,B and C are " << A << "," << B << "," << C << endl;
	if (A > B)
	{
		if (A > C)
		{
			cout << "The max is A." << endl;
		}
		else
		{
			cout << "The max is C." << endl;
		}
	}
	else
	{
		if (B > C)
		{
			cout << "The max is B." << endl;
		}
		else
		{
			cout << "The max is C." << endl;
		}
	}
}

3、三目运算符
语法:表达式?表达式1:表达式2;
意义:表达式True,执行表达式1,否则执行表达式2
本质是运算符,结果还是变量,仍然可以赋值
(变量1 > 变量2 ?变量1:变量2)=变量值
意思是原本变量1和变量2比较,哪个大就选择哪个变量,再将这个变量以新的变量值赋值。

4、switch语句
也是条件结构的一种
只能是整型或者字符型判断
不能判断区间
可以将浮点区间编码成整型变量再执行任务
跟C语言也差不多
注意:一个case最后面一定要配套break;否则执行完这个case之后,编译器会继续执行下面的case,直到结束!
switch语法结构
5、循环结构while
语法:while (循环条件) { 循环体 }
用于重复的语句,便于减少代码量
循环条件成立,执行循环体;不成立时跳出。
break; 断开循环体;
continue; 断开此次循环体,进入下一次循环。

6、案例1:猜数字游戏
系统随机生成1~100整数
用户不断输入整数
系统反馈“数值太大”/“数值太小”
直到用户输入正确了

#include <iostream>
using namespace std;
#include <ctime>

int main() 
{
	// 生成随机数种子,防止每次执行程序时都生成同样的随机数
	// 使用系统时间,需要包含与时间有关的头文件,否则会报错
	srand((unsigned int)time(NULL));

	// 系统随机生成一个1~100范围内的整数
	int true_number = 0;
	true_number = rand() % 100 + 1;

	// 初始化猜测结果,初始化为false
	bool flag = false;

	while (!flag) 
	{
		// 进入循环,在循环内反复猜测
		int guess_number = 0;
		cout << "Please input a number! " << endl;
		cin >> guess_number;
		cout << "The number you input is: " << guess_number << endl;

		if (guess_number > true_number) 
			{
				cout << "The number you input is larger!" << endl;
				flag = false;
			}
		else if (guess_number < true_number)
			{
				cout << "The number you input is smaller!" << endl;
				flag = false;
			}
		else
			{
				cout << "Congratulations! You guess the true number!" << endl;
				flag = true;
			}
	}
	system("pause");
	return 0;
}

6、循环结构:do while语句
语法:do {代码块} while ( 循环条件 )
特点:先执行一次代码块在判断循环条件

7、案例2:寻找水仙花数
水仙花数是指一个三位数
它的百位、十位和个位的三次幂相加仍等于他自己。

#include <iostream>
using namespace std;


int main() 
{
	int num = 100;
	int gewei = 0;
	int shiwei = 0;
	int baiwei = 0;
	while (num < 999) 
	{
		gewei = num % 10;
		shiwei = (num / 10) % 10;
		baiwei = num / 100;
		if ((gewei*gewei*gewei + shiwei*shiwei*shiwei + baiwei*baiwei*baiwei) == num)
			{
			cout << "The number " << num << " is a Narcissistic Number!" << endl;
			}
		num++;
	}
	system("pause");
	return 0;
}

8、循环结构:for语句
语法:for( 起始表达式; 循环条件; 末尾循环体; ) { 代码块 }
中间的三个表达式,倘若在前面提到了,可以不写

9、案例三:敲桌子游戏
在0~100中,凡是出现数字7的,或者是7的倍数,均要“敲桌子”

#include <iostream>
using namespace std;


int main() 
{
	int i;
	int gewei = 0;
	int shiwei = 0;
	int baiwei = 0;
	for (i = 0; i <= 100; i++) 
	{
		gewei = i % 10;
		shiwei = (i / 10) % 10;
		baiwei = i / 100;
		if ((i % 7 == 0) || ((gewei == 7) || (shiwei == 7) || (baiwei == 7)) )
		{
			cout << "Hit the Desk!" << endl;
		}
		else
		{
			cout << i << endl;
		}
	}
	system("pause");
	return 0;
}

10、嵌套循环
也就是在循环体中再增加一次循环
外层执行一次,内层执行一周

#include <iostream>
using namespace std;


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

11、案例四:乘法口诀表

#include <iostream>
using namespace std;


int main() 
{
	int i, j;
	for (i = 1; i <= 9; i++) 
	{
		for (j = 1; j <= i; j++) 
		{
			cout << j << "*" << i << "=" << j * i << "\t";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

输出的结果如下:

1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81
请按任意键继续. . .

12、break/continue/go_to语句归纳
break:在switch…case…是跳出本case;在循环语句内是跳出本循环语句块;在嵌套循环内表示跳出就近的循环语句块。
continue:跳过本次循环,执行新一次循环。
go_to:无条件跳转代码,频繁使用go_to容易造成程序崩溃。使用go_to语句时后面要增加标记,需要在后面补上对标记的定义。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl+Alt+L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值