C++ 程序流程结构 (选择结构,循环结构,顺序结构)
C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
1.顺序结构:程序按顺序执行,不发生跳转
2.选择结构:依据条件是否满足,有选择的执行相应功能
3.循环结构:依据条件是否满足,循环多次执行某段代码
选择结构
选择结构 if语句的三种形式:·单行格式if语句 多行格式if语句 多条件的if语句
单行 if语句
需求:用户输入一个分数,并显示该分数,如果大于600分就显示考上了一本大学,不大于600分就显示未考上一本大学
#include<iostream>
using namespace std;
#include <string>
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
}
else
{
cout << "很遗憾您未被一本大学录取" << endl;
}
system("pause");
return 0;
}
多条件 if语句
需求:大于600 被一本录取,大于500被二本录取,大于400被三本录取,不大于400 都显示未考上大学
注:分数仅为举例,无任何不良引导(狗头保命)
#include<iostream>
using namespace std;
#include <string>
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
}
else if (score > 500)
{
cout << "恭喜您被二本大学录取" << endl;
}
else if (score > 400)
{
cout << "恭喜您被三本大学录取" << endl;
}
else
{
cout << "很遗憾您未被大学录取" << endl;
}
system("pause");
return 0;
}
嵌套if语句
在上一道题基础上,加上条件:大于700 考北大;大于650 考清华,大于600 考人大
#include<iostream>
using namespace std;
#include <string>
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
if (score > 700)
{
cout << "恭喜您考入北京大学" << endl;
}
else if (score > 650)
{
cout << "恭喜您考入清华大学" << endl;
}
else
{
cout << "恭喜您考入人民大学" << endl;
}
}
else if(score > 500 )
{
cout << "恭喜您被二本大学录取" << endl;
}
else if (score > 400)
{
cout << "恭喜您被三本大学录取" << endl;
}
else
{
cout << "很遗憾您未被大学录取" << endl;
}
system("pause");
return 0;
}
选择结构案例-三只小猪称体重
需求:假设有a b c三只小猪,比较出最沉的小猪
#include<iostream>
using namespace std;
#include <string>
int main() {
int num1 = 0;
int num2 = 0;
int num3 = 0;
cout << "请输入小猪a的体重" << endl;
cin >> num1;
cout << "请输入小猪b的体重" << endl;
cin >> num2;
cout << "请输入小猪c的体重" << endl;
cin >> num3;
cout << "小猪a的体重" << num1 << endl;
cout << "小猪b的体重" << num2 << endl;
cout << "小猪c的体重" << num3 << endl;
if (num1 > num2)
{
if (num1 > num3)
{
cout << "小猪a最重" << endl;
}
else
{
cout << "小猪c最重" << endl;
}
}
else
{
if (num2 > num3)
{
cout << "小猪b最重" << endl;
}
else
{
cout << "小猪c最重" << endl;
}
}
system("pause");
return 0;
}
选择结构案例-三目运算符
三目运算符语法 表达式1?:表达式2:表达式3
如果表达式1的值为真,执行表达式2,并返回表达式2的结果; 如果表达式1的值为假,执行表达式3,并返回表达式3的结果.
#include<iostream>
using namespace std;
#include <string>
int main() {
int a = 20;
int b = 12;
int c = 14;
c = (a > b ? a : b);
cout << "c =" << c << endl;
//在c++中的三目运算符返回的是变量可以继续赋值
(a > b ? a : b) = 100;
cout << "a =" << a << endl;
cout << "b =" << b << endl;
system("pause");
return 0;
}
选择结构案例-switch语句
需求:给电影评分,9-10经典电影 8-7电影非常好 5-6一般 4及以下是烂片
#include<iostream>
using namespace std;
#include <string>
int main() {
int score = 0;
cout << "请输入你的电影评分" << endl;
cin >> score ;
cout << "您输入的电影评分是" << score << endl;
switch (score)
{
case 10:
cout << "您认为该电影是经典电影" << endl;
break;
case 9:
cout << "您认为该电影是经典电影" << endl;
break;
case 8:
cout << "您认为该电影非常好" << endl;
break;
case 7:
cout << "您认为该电影非常好" << endl;
break;
case 6:
cout << "您认为该电影一般" << endl;
break;
case 5:
cout << "您认为该电影一般" << endl;
break;
default:
cout << "您认为该电影是烂片" << endl;
break;
}
//switch优缺点:缺点是不能范围区间判断 优点是逻辑清晰,执行效率高
system("pause");
return 0;
}
循环结构
while循环语句
需求:输出0-9这些数字
#include<iostream>
using namespace std;
#include <string>
int main() {
int num = 0;
while (num < 10)
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
循环结构案例-猜数字小游戏
需求:猜一个100以内的数字
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
//添加随机数种子 作用利用当前系统时间生成随机数,防止每次随机数一样
srand((unsigned int)time(NULL));
//1.系统生成随机数
int num = rand() % 100 + 1;
//生成0+1-99+1随机数
//2.玩家猜测
int val = 0;
while (1)
{
cin >> val;
//3.判断玩家的猜测
if (val > num)
{
cout << "猜大了" << endl;
}
else if (val < num)
{
cout << "猜小了" << endl;
}
else
{
cout << "猜对了" << endl;
break;//退出当前循环
}
}
system("pause");
return 0;
}
do while语句
语法:do{循环语句}while(循环条件)
注意:与while的区别在于do…while会先执行一次循环语句,再判断循环条件
需求:打印0-9这些数
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
int num = 0;
do
{
cout << num << endl;
num++;
}
while (num < 10);
system("pause");
return 0;
}
循环结构案例-水仙花数
水仙花案例 案例描述:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身例如:1 ^ 3 + 5 ^ 3 + 313 = 153,要求:求出所有三位数的水仙花数
步骤分析:1.将所有的三位数都输出 2.在这个数字中找到水仙花数
获取个位 对数字取模于10,可以获取到个位
获取十位 先除以10,得到两位数,再取模于10
获取百位 除以100
最后判断
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
int num = 100;
do
{
int a = 0;
int b = 0;
int c = 0;
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++;
}
while (num < 1000);
system("pause");
return 0;
}
循环结构-for循环
for循环语句 语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}
需求:打印0-9这些数字
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
循环结构案-敲桌子
敲桌子游戏 案例描述:从1开始数到数字10o,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。
关键步骤:
- 先输出1到100
2.从这100个数字中找到特殊数字
特殊数字 7的倍数 %7=0; 个位有7 %10=7; 十位有7 /10=7 这些情况打印敲桌子
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
for (int i = 1; i <= 100; i++)
{
if (i % 7 == 0 || i % 10 == 7|| i/ 10 == 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
嵌套循环
需求:嵌套循环 利用嵌套循环打印星图
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
//外层循环
for (int i = 0; i < 10; i++)
{ //内层循环
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
嵌套循环案例-打印乘法口诀表
关键点:列数乘以行数=计算结果 而且列数<=当前的行数
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
for (int i = 1; i < +9; i++)
{
//cout << i << endl;
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << j * i << " ";
}
cout << endl;
}
system("pause");
return 0;
}
跳转语句
跳转语句-break语句使用的时机:
1.出现在switch条件语句中,作用是终止case并跳出switch
需求:选择游戏困难程度等级
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
cout << "请选择游戏的难度" << endl;
cout << "1.普通" << endl;
cout << "2.中等" << endl;
cout << "3.困难" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
cout << "您选择的是普通难度" << endl;
break;
case 2:
cout << "您选择的是中等难度" << endl;
break;
case 3:
cout << "您选择的是困难难度" << endl;
break;
default:
break;
}
system("pause");
return 0;
}
2.出现在循环语句中,作用是跳出当前的循环语句
需求:仅打印5以内的数字
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
system("pause");
return 0;
}
3.出现在嵌套循环中,跳出最近的内层循环语句
需求:打印十行五列星图
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
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语句
continue在循环语句中,跳过被次循环中余下尚未执行的语句,继续执行下一次循环
需求:仅输出100以内的奇数不打印偶数
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)//输出奇数,不输出偶数
{
continue;//可以筛选条件,执行到此就不再向下执行,执行下一次循环
}
cout << i << endl;
}
system("pause");
return 0;
}
跳转语句- go to语句
需求:打印数字1,直接打印数字5,跳过中间那些数字
#include<iostream>
using namespace std;
#include <string>
#include <ctime>
int main() {
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG:
cout << "5" << endl;
system("pause");
return 0;
}
本次分享为b站黑马程序员C++系列教程的学习笔记,后续会继续更新,如有问题,欢迎交流。