目录
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
- 顺序结构:程序按顺序执行,不发生跳转
- 选择结构:依据条件是否满足,有选择的执行相应功能
- 循环结构:依据条件是否满足,循环多次执行某段代码
1 选择结构
1.1 if语句
作用:执行满足条件的语句
if语句的三种形式
- 单行格式if语句
- 多行格式if语句
- 多条件的if语句
单行格式if语句:if(条件){条件满足执行的语句}
#include<iostream>
using namespace std;
/*
选择结构 单行if语句
*/
int main() {
//用户输入分数,如果分数大于60,视为考上一本大学,在屏幕上输出
//1.用户输入分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断分数是否大于600,如果大于600,那么输出
if (score > 600)
{
cout << "Congratulations on entering a key university" << endl;
}
system("pause");
return 0;
}
多行格式if语句:if(条件){条件满足执行的语句} else{条件不满足执行的语句}
#include<iostream>
using namespace std;
/*
选择结构 多行if语句
*/
int main() {
//输入考试分数,如果大于600,视为考上一本,在屏幕输出
//如果没考上一本大学,打印未考上一本大学
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断
if (score > 600)
{
cout << "Congratulations on entering a key university" << endl;
}
else
{
cout << "sorry,you didn't get into a key university " << endl;
}
system("pause");
return 0;
}
多条件的if语句:if(条件1){执行满足条件1的语句} else if(条件2){执行满足条件2的语句}......else{执行条件都不满足的语句}
#include<iostream>
using namespace std;
/*
选择结构 多条件if语句
*/
int main() {
//输入一个考试分数,如果大于600分,视为考上一本大学,在屏幕输出
//大于500,视为考上二本
//大二400,视为考上三本
//小于等于400,视为为考上本科,在屏幕输出
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << 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;
}
嵌套if语句:在if语句中,可以嵌套使用if语句,达到更精确的条件判断。
案例需求:
- 提示用户输入一个高考考试分数,根据分数做如下判断;
- 分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科;
- 在一本分数中如果大于700分,考入北大,大于650分,考入清华,大于600考入人大。
#include<iostream>
using namespace std;
/*
嵌套if语句
*/
int main() {
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << 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;
}
练习案例:三只小猪称体重
有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪知小猪最重。
#include<iostream>
using namespace std;
/*
输入三只小猪的体重,判断谁最重
*/
int main() {
//1.分别输入三只小猪的体重
int A = 0;
cout << "请输入小猪A的体重:" << endl;
cin >> A;
int B = 0;
cout << "请输入小猪B的体重:" << endl;
cin >> B;
int C = 0;
cout << "请输入小猪C的体重:" << endl;
cin >> C;
cout << "小猪A的体重为" << A << endl;
cout << "小猪B的体重为" << B << endl;
cout << "小猪C的体重为" << C << endl;
//2.if语句判断
if (A > B)
{
if (A > C)
{
cout << "小猪A最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
else
{
if (B > C)
{
cout << "小猪B最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
system("pause");
return 0;
}
1.2 三目运算符
作用:通过三木运算符实现简单的判断
语法:表达式1?表达式2:表达式3
解释:
- 如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
- 如果表达式1的值为假,执行表达式3,并返回表达式3的结果。
#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 = " << c << endl;
//在C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
1.3 switch语句
作用:执行多条件分支语句
switch与if相比
缺点:判断时候只能是整型或者字符型,不可以是一个区间
优点:结构清晰,执行效率高。
#include<iostream>
using namespace std;
/*
switch语句
*/
int main() {
//给一个电影打分
//10~9经典
//8~7非常好
//6~5一般
//5以下烂片
//1.提示用户进行评分
int score = 0;
cout << "请输入您的电影评分:" << endl;
cin >> score;
cout << "您的电影评分为:" << score << endl;
//2.switch
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;
}
2 循环结构
2.1 while循环语句
作用:满足循环条件,执行循环语句
语法:while(循环条件){循环语句}
解释:只要循环条件的结果为真,就执行循环语句
注意事项:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环
#include<iostream>
using namespace std;
/*
while循环
*/
int main() {
//在屏幕中打印0~9这10个数
int num = 0;
while (num < 10)
{
cout << num++ << endl;
}
system("pause");
return 0;
}
练习案例:猜数字
#include<iostream>
#include<ctime>
using namespace std;
/*
猜数字
*/
int main() {
//添加随机种子,利用当前系统时间生成随机数,防止每次随机数都一样
srand((unsigned int)time(NULL));
//系统生成随机数
int num = rand() % 100 + 1;//rand() % 100生成0~99的随机数
//玩家猜测
int val = 0;
int i = 1;
while (i)
{
cout << "请输入您猜测的数字:" << endl;
cin >> val;
//判断结果
if (num > val)
{
cout << "猜小了" << endl;
}
else if (num < val)
{
cout << "猜大了" << endl;
}
else
{
cout << "猜对了" << endl;
i = 0;
}
}
system("pause");
return 0;
}
2.2 do...while循环语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件)
注意事项:与while的区别在于do...while会先执行一次循环语句,再判断循环条件
#include<iostream>
using namespace std;
/*
do_while语句
*/
int main() {
//在屏幕中输出0~9是个数字
int num = 0;
do
{
cout << num++ << endl;
} while (num < 10);
system("pause");
return 0;
}
练习案例:利用do...while语句完成水仙花数代码
#include<iostream>
using namespace std;
/*
水仙花数是指一个三位数,他的每个位上的数字的3次幂之和等于它本身
*/
int main() {
//生成一个数
int num = 100;
int a = 0;//百位
int b = 0;//十位
int c = 0;//个位
//循环找数
do
{
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if (a * a * a + b * b * b + c * c * c == num)
{
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
2.3 for循环语句
作用:满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}
#include<iostream>
using namespace std;
/*
for循环语句
*/
int main() {
//在屏幕输出0~9是个数字
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
练习案例:敲桌子
#include<iostream>
using namespace std;
/*
敲桌子
从1开始疏导数字100,如果数字个位含有7,或者数字十位含有7
或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出
*/
int main() {
//定义特征变量
int a = 0;//个位
int b = 0;//十位
int c = 0;//除7后的余数
//直接for循环
for (int i = 1; i < 101; i++)
{
a = i % 10;
b = i / 10;
c = i % 7;
if (a == 7 | b == 7 | c == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
2.4 嵌套循环
作用:在循环体中在嵌套一层循环,解决一些实际问题
#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;
}
练习案例:乘法口诀表
#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;
- 出现在循环语句中,作用是跳出当前的循环语句;
- 出现在嵌套循环中,跳出最近的内层循环语句。
#include<iostream>
using namespace std;
/*
break的使用时机
*/
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++)
{
//如果i=5,推出循环,不再打印
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;
}
3.2 continue语句
作用:在循环语句中,跳过本次循环中余下还未执行的语句,继续执行下一次循环
#include<iostream>
using namespace std;
/*
continue语句
*/
int main() {
for (int i = 0; i <= 100; i++)
{
//如果是奇数输出,偶数不输出
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
3.3 goto语句
作用:可以无条件跳转语句
语法:goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include<iostream>
using namespace std;
/*
goto语句
*/
int main() {
cout << "1.xxxx" << endl;
cout << "2.xxxx" << endl;
cout << "3.xxxx" << endl;
goto FLAG;
cout << "4.xxxx" << endl;
cout << "5.xxxx" << endl;
cout << "6.xxxx" << endl;
cout << "7.xxxx" << endl;
FLAG:
cout << "8.xxxx" << endl;
system("pause");
return 0;
}