1、选择结构
1.1 if语句
1.1.1 单行格式if语句
示例:
//1、单行格式if语句: if(条件){条件满足执行的语句}
#include<iostream>
using namespace std;
int main(){
//选择结构-单行if语句
//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印
int score = 0;
cout << "请输入一个分数: " << endl;
cin >> score;
cout << "您输入的分数是: " << score << endl;
//if语句
//注意事项,在if判断语句后面,不要加分号
if (score > 600)
{
cout << "恭喜您考上一本大学!" << endl;
}
system("pause");
return 0;
}
输出:
1.1.2 多行格式if语句
示例:
//2、多行if语句:if(条件){条件满足执行的语句}else{条件不满足执行的语句};
#include<iostream>
using namespace std;
int main(){
int score = 0;
cout << "请输入一个分数: " << endl;
cin >> score;
if (score > 600)
{
cout << "您考上了一本大学!" << endl;
}
else
{
cout << "您未考上一本大学!" << endl;
}
system("pause");
return 0;
}
1.1.3 多条件if语句
示例:
//多条件if语句:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句}...else{都不满足执行的语句}
#include<iostream>
using namespace std;
int main(){
int score = 0;
cout << "请输入考试分数: " << endl;
cin >> score;
if (score > 600)
{
cout << "您考上了一本大学" << endl;
}
else if (score > 500)
{
cout << "您考上了二本大学" << endl;
}
else if (score > 400)
{
cout << "您考上了三本大学" << endl;
}
else
{
cout << "您落榜了,明年加油" << endl;
}
system("pause");
return 0;
}
输出:
1.1.4 嵌套if语句
示例:
/*案例需求:
提示用户输入一个高考考试分数,根据分数做如下判断
分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科;
在一本分数中,如果大于700分,考入北大,大于650分,考入清华,大于600考入人大。*/
#include<iostream>
using namespace std;
int main(){
int score = 0;
cout << "请输入考试分数: " << endl;
cin >> score;
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;
}
输出:
1.1.5 练习:三只小猪称体重
//练习案例:
//有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重
#include<iostream>
using namespace std;
int main(){
int doga = 0;
int dogb = 0;
int dogc = 0;
int max = 0;
cout << "请输入小猪A的体重:" << endl;
cin >> doga;
cout << "请输入小猪B的体重:" << endl;
cin >> dogb;
cout << "请输入小猪C的体重:" << endl;
cin >> dogc;
if (doga > dogb)
{
max = doga;
if (max > dogc)
{
cout << "最重的是小猪A: " << doga << endl;
}
else
{
cout << "最重的是小猪C:" << dogc << endl;
}
}
else
{
max = dogb;
if (max > dogc)
{
cout << "最重的是小猪B:" << dogb << endl;
}
else
{
cout << "最重的是小猪C:" << dogc << endl;
}
}
system("pause");
return 0;
}
输出:
1.2 三目运算符
1.2.1 三目运算符
示例:
//三目运算符,通过三目运算符实现简单的判断
//表达式:表达式1?表达式2:表达式3
//如果表达式1的值为真,执行表达式2,并返回表达式2的结果
//如果表达式1的值为假,执行表达式3,并返回表达式3的结果
#include<iostream>
using namespace std;
int main(){
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;//C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
system("pause");
return 0;
}
输出:
1.2.2 使用三目运算符称小猪体重
示例:
#include<iostream>
using namespace std;
int main(){
int doga = 0;
int dogb = 0;
int dogc = 0;
int max = 0;
cout << "请输入小猪A的体重:" << endl;
cin >> doga;
cout << "请输入小猪B的体重:" << endl;
cin >> dogb;
cout << "请输入小猪C的体重:" << endl;
cin >> dogc;
max = doga > dogb ? doga : dogb;
max = max > dogc ? max : dogc;
cout << "最大的体重是: " << max << endl;
system("pause");
return 0;
}
输出:
1.3 switch语句
示例:
//作用:执行多条件分支语句
/*语法:
switch(表达式) {
case 结果1:执行语句;break;
case 结果2:执行语句;break;
... default:执行语句;break; }
*/
#include <iostream>
using namespace std;
int main(){
int score = 0;
cout << "请给电影评分: " << endl;
cin >> score;
cout << "您输入的分数是: " << score << endl;
switch (score)
{
case 10:
case 9:
cout << "经典" << endl;
break;
case 8:
case 7:
cout << "非常好" << endl;
break;
case 6:
case 5:
cout << "一般" << endl;
default:
cout << "烂片" << endl;
break;
}
system("pause");
return 0;
}
//注意1:switch语句中表达式类型只能是整型或者字符型
//注意2:case里如果没有break,那么程序会一直向下执行
//总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间
输出:
2、循环结构
2.1 while循环语句
2.1.1 while循环语句
示例:
//语法:while(循环条件){循环语句}
//只要循环条件的结果为真,执行循环语句
#include<iostream>
using namespace std;
int main(){
int num = 0;
while (num < 10)
{
cout << "num = " << num << endl;
num++;
}
system("pause");
return 0;
}
输出:
2.1.2 while循环练习:猜数字
//案例描述:系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过
//小,如果猜对恭喜玩家胜利,并且退出游戏。
#include<iostream>
using namespace std;
#include <ctime>//系统时间头文件
int main(){
srand((unsigned int)time(NULL));//添加随机数种子,利用系统时间生成随机数
int num = rand() % 100 + 1;//rand() % 100生成0~99的随机数
//cout << num << endl;
int val = 0;
cout << "请在 0~100 之间猜一个数字" << endl;
while (1)
{
cin >> val;
if (val > num)
{
cout << "大了!" << endl;
}
else if (val < num)
{
cout << "小了!" << endl;
}
else
{
cout << "猜对了!" << endl;
break;
}
}
system("pause");
return 0;
}
输出:
2.2 do…while循环语句
2.2.1 do…while
示例:
//语法:do{循环语句} while(循环条件);
//先执行循环语句,再判断循环条件
#include<iostream>
using namespace std;
int main(){
int num = 0;
do
{
cout << num << endl;
num++;
}while(num < 10);
system("pause");
return 0;
}
输出:
注意:
do…while与while的区别在于:
do…while会先执行一次循环语句,然后再判断循环条件
2.2.2 do…while练习:水仙花数
/* 案例描述:
水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
例如:1^3 + 5^3+ 3^3 = 153
请利用do...while语句,求出所有3位数中的水仙花数*/
#include<iostream>
using namespace std;
int main(){
int num = 100;
do
{
int a = 0;
int b = 0;
int c = 0;
a = num / 100;
b = (num % 100) / 10;
c = num % 10;
if (num == a * a * a + b * b * b + c * c * c)
{
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
输出:
2.3 for循环语句
2.3.1 for循环
示例:
//语法:for(起始表达条件;条件表达式;末尾循环体){循环语句;}
#include<iostream>
using namespace std;
int main(){
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
输出:
2.3.2 for循环练习:敲桌子游戏
/*案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,
我们打印敲桌子,其余数字直接打印输出。*/
#include<iostream>
using namespace std;
int main(){
for (int i = 1; i < 100; i++)
{
if (i / 10 == 7)//十位数上有7
{
cout << " 敲桌子 ";
}
else if (i % 7 == 0)//7的倍数
{
cout << " 敲桌子 ";
}
else if (i % 10 == 7)//个位数是7
{
cout << " 敲桌子 ";
}
else
{
cout << " " << i << " ";
}
}
cout << endl;
system("pause");
return 0;
}
//多个条件可以使用||比如:(i / 10 == 7 || i % 7 == 0 || i % 10 == 7)
输出:
2.4 嵌套循环
2.4.1 嵌套循环
示例:
//作用: 在循环体中再嵌套一层循环,解决一些实际问题
#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;
}
输出:
2.4.2 乘法口诀表
案例:利用嵌套循环,实现乘法口诀表
//案例描述:利用嵌套循环,实现九九乘法表
#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;
}
输出:
3、跳转语句
3.1 break 语句
作用:用于跳出选择结构或者循环结构
break使用的时机:
出现在switch条件语句中,作用是终止case并跳出switch
出现在循环语句中,作用是跳出当前的循环语句
出现在嵌套循环中,跳出最近的内层循环语句
示例1:出现在switch条件语句中
#include<iostream>
using namespace std;
int main(){
//1、在switch语句中使用break
cout << "请选择您挑战副本的难度:" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
int num = 0;
cin >> num;
switch (num)
{
case 1:
cout << "您选择的是普通难度" << endl;
break;
case 2:
cout << "您选择的是中等难度" << endl;
break;
case 3:
cout << "您选择的是困难难度" << endl;
break;
}
system("pause");
return 0;
}
输出:
示例2:出现在循环语句中
#include<iostream>
using namespace std;
int main(){
//2、在循环语句中用break
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
system("pause");
return 0;
}
输出:
示例3:出现在嵌套循环中
#include<iostream>
using namespace std;
int main(){
//3、在嵌套循环语句中使用break,退出内层循环
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;
}
输出:
3.2 continue 语句
//在循环语句中,跳过本次循环中尚未执行的语句,继续执行下一次循环
#include<iostream>
using namespace std;
int main(){
for (int i = 0; i < 100; i++)
{
if (i % 2 ==0)
{
continue;
}
cout << " " << i << " ";
}
cout << endl;
system("pause");
return 0;
}
//注意:continue并没有使整个循环终止,而break会跳出循环
输出:
3.3 goto 语句
示例:
//可以无条件跳转语句
//语法: goto 标记;
//解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include <iostream>
using namespace std;
int main(){
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
cout << "5" << endl;
FLAG:
cout << "6" << endl;
system("pause");
return 0;
}
输出: