C++基础入门---4.程序流程结构【P24~P41】
4. 程序流程结构
4.1 选择结构
4.1.1 单行if语句
注:if条件语句后面不要加分号!!!
#include<iostream>
using namespace std;
int main()
{
//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
//1、用户输入分数
int score = 0;
cout << "请输入高考分数 score= "<< endl;
cin >> score;
//2、打印用户的分数
cout << "您输入的分数为: " << score << endl;
//3、判断分数是否大于600,如果大于,那么输出
if (score >= 600)
{
cout << "恭喜您考上了一本大学!" << endl;
}
system("pause");
return 0;
}
4.1.2 多行if语句
#include<iostream>
using namespace std;
int main()
{
//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
//如果没考上一本大学,打印未考上一本大学
//1、用户输入分数
int score = 0;
cout << "请输入高考分数 score= " << endl;
cin >> score;
//2、打印用户的分数
cout << "您输入的分数为: " << score << endl;
//3、判断分数是否大于600,如果大于,那么输出
if (score >= 600)
{
cout << "恭喜您考上了一本大学!" << endl;
}
else
{
cout << "很遗憾,您未考上一本大学。" << endl;
}
system("pause");
return 0;
}
4.1.3 多条件if语句
#include<iostream>
using namespace std;
int main()
{
//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
//大于500,视为考上二本大学,输出
//大于400,视为考上三本大学,输出
//小于等于400,视为未考上大学
//1、用户输入分数
int score = 0;
cout << "请输入高考分数 score= " << endl;
cin >> score;
//2、打印用户的分数
cout << "您输入的分数为: " << score << endl;
//3、判断
if (score > 600)
{
cout << "恭喜您考上了一本大学!" << endl;
}
else if(score>500)
{
cout << "恭喜您考上了二本大学!" << endl;
}
else if (score > 400)
{
cout << "恭喜您考上了三本大学!" << endl;
}
else
{
cout << "很遗憾,您未考上大学。" << endl;
}
system("pause");
return 0;
}
4.1.4 嵌套if语句
#include<iostream>
using namespace std;
int main()
{
//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
//大于700,北大
//大于650,清华
//大于600,人大
//大于500,视为考上二本大学,输出
//大于400,视为考上三本大学,输出
//小于等于400,视为未考上大学
//1、用户输入分数
int score = 0;
cout << "请输入高考分数 score= " << endl;
cin >> score;
//2、打印用户的分数
cout << "您输入的分数为: " << score << endl;
//3、判断
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;
}
4.1.5 案例-三只小猪称体重
# include<iostream>
using namespace std;
int main()
{
//1、创建三只小猪的体重变量
int num1 = 0;
int num2 = 0;
int num3 = 0;
//2、用户输入三只小猪的重量
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;
//3、判断哪一只最重
if (num1 > num2)//A比B重
{
if (num1 > num3)//A比B重,且A比C重
{
cout << "小猪A最重" << endl;
}
else//A比B重,且C比A重
{
cout << "小猪C最重" << endl;
}
}
else//B比A重
{
if (num2 > num3)//B比A重,且B比C重
{
cout << "小猪B最重" << endl;
}
else//B比A重,且C比B重
{
cout << "小猪C最重" << endl;
}
}
system("pause");
return 0;
}
4.1.6 案例-三目运算符
#include<iostream>
using namespace std;
int main()
{
//三目运算符
//创建三个变量a,b,c
//将a和b作比较,将变量大的赋值给c
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
cout << c << endl;
//在c++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
system("pause");
return 0;
}
4.1.7 switch语句
if和switch区别?
switch缺点: 判断时只能是整形或者字符型,不可以是一个区间
switch优点: 结构清晰,执行效率高
#include<iostream>
using namespace std;
int main()
{
//为电影评分
//10-9 经典 8-7 非常好 7-6 一般 5以下 烂片
//1、提示用户为电影评分
cout << "请给电影打分" << endl;
//2、用户开始打分
int score = 0;
cin >> score;
cout << "您为电影打的分数是:" << score << endl;
//3、根据用户打分输出电影结果
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;
}
system("pause");
return 0;
}
4.2 循环结构
4.2.1 while语句
注:在写循环时要注意避免死循环的出现
#include<iostream>
using namespace std;
int main()
{
int num = 0;
while (num < 10)
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
4.2.2 案例-猜数字
#include<iostream>
#include<ctime>//系统时间头文件
using namespace std;
int main()
{
//0、添加随机数种子,利用当前系统时间生成随机数,防止每次随机数一样
srand((unsigned int)time(NULL));
//1、系统生成1-100随机数
int num = rand() % 100 + 1;
//2、玩家进行猜测
int val = 0;
while (1)
{
cout << "请您输入猜测数据:" << endl;
cin >> val;
//3、判断玩家的猜测
if (val > num)
{
cout << "猜测过大" << endl;
}
else if (val < num)
{
cout << "猜测过小" << endl;
}
else
{
cout << "恭喜您猜对了!" << endl;
break;
}
}
system("pause");
return 0;
}
4.2.3 do…while语句
#include<iostream>
using namespace std;
/*
int main()
{
int num = 0;
while (num < 10)
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
*/
int main()
{
int num = 0;
do
{
cout << num << endl;
num++;
}
while (num < 10);
system("pause");
return 0;
}
4.2.4 案例-水仙花数
#include<iostream>
using namespace std;
int main()
{
//1、先打印所有三位数字
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;
}
4.2.5 for循环
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
4.2.6 案例-敲桌子
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 100; i++)
{
//从1-100中找到特殊数字,打印“敲桌子”
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
4.2.7 嵌套循环
#include<iostream>
using namespace std;
int main()
{
//外层执行一次,内层执行一周
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
4.2.8 嵌套循环案例-乘法口诀表
【分析】
# include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
4.3 跳转语句
4.3.1 break语句
#include<iostream>
using namespace std;
int main()
{ /*
//1、出现在switch语句中
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;
}
*/
//2、出现在嵌套循环语句中
/*
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
*/
//3、出现在嵌套循环语句中
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;
}
4.3.2 continue语句
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
continue;//可以筛选条件,执行到此就不在向下执行。
//break会跳出大循环而continue会跳出当前循环,执行下一次循环
}
cout << i << endl;
}
system("pause");
return 0;
}
4.3.3 goto语句
#include<iostream>
using namespace std;
int main()
{
cout << "1.xxxx" << endl;
cout << "2.xxxx" << endl;
goto FLAG;
cout << "3.xxxx" << endl;
FLAG:
cout << "4.xxxx" << endl;
cout << "5.xxxx" << endl;
system("pause");
return 0;
}