目录
一、选择结构
1.if语句
- 单行格式if语句
if(条件){ 条件满足执行的语句 } |
- 多行格式if语句
if(条件){ 条件满足执行的语句 } else{ 条件不满足执行的语句} |
- 多条件的if语句
if(条件1){ 条件1满足执行的语句 } else if(条件2){ 条件2满足执行的语句 } …… else{ 都不满足执行的语句 } |
注意:
- if语句中的表达式可以是符合C++语法规则的任一表达式,可以是逻辑、关系、算术表达式
- else子句不能作为语句单独使用,它必须是if语句的一部分,与if配对使用
- if、else的配对原则:else总是与他最近的还没有配对的if语句相配对
选择结构案例-三只小猪称体重:
#include<iostream>
#include<string>
using namespace std;
//三只小猪称体重,并判断哪只小猪最重。
int main()
{
int A,B,C;
cout<<"请分别输入三只小猪A,B,C的体重"<<endl;
cin>>A>>B>>C;
cout<<"三只小猪的体重分别是: "<<A<<'\t'<<B<<'\t'<<C<<endl;
if(A>B)
{
if(A>C)
{
cout<<"A最重"<<endl;
}
else cout<<"C最重"<<endl;
}
else
{
if(B>C)
{
cout<<"B最重"<<endl;
}
else cout<<"C最重"<<endl;
}
return 0;
}
2.三目运算符
表达式1?表达式2:表达式3 |
注意:
- 若表达式1的值为真,执行表达式2,并返回表达式2的结果
- 若表达式1的值为假,执行表达式3,并返回表达式3的结果
- 条件运算符的结合方向为“自右向左”
a>b?a:c>d?c:d = a>b?a:(c>d?c:d)
- 三目运算符返回的是变量,可以继续赋值
#include<iostream>
using namespace std;
int main(){
int a = 10;
int b = 20;
(a>b?a:b)=100;
cout<<a<<'\t'<<b<<endl;
//a=10;b=100
return 0;
}
3.switch语句
switch(表达式) { case 结果1:执行语句;break; case 结果1:执行语句;break; …… default:执行语句;break; } |
- 优点:结构清晰,执行效率高
- 缺点:判断时只能是整型或字符型,不可以是一个区间
- case若无break,程序会一直向下运行
#include<iostream>
using namespace std;
int main(){
int score;
cout<<"输入评分"<<endl;
cin>>score;
cout<<"您的评分为"<<score<<endl;
switch(score)
{
case 10:
cout<<"经典\n";
break;
//break:退出当前分支,如无break,会一直向下运行。
//如输入评分10,结果:经典 经典 良好 接受
case 9:
cout<<"经典\n";
break;
case 8:
cout<<"良好\n";
break;
default:cout<<"接受\n";
}
return 0;
}
二、循环结构
1.while循环语句
while(循环条件){ 循环语句 } |
while循环练习案例:猜数字
#include<iostream>
using namespace std;
//time系统头文件的包含
#include<ctime>
//系统随机生成一个1到100的数字,玩家进行猜测
//如果猜错,提示玩家数字过大或过小;如果猜对,恭喜玩家胜利,并退出游戏。
int main()
{
srand((unsigned int)time(NULL));//伪随机数种子
int num = rand()%100+1;
//rand()%(b-a+1)+a表示 a到b范围内随机数字(伪随机数,要添加随机数种子)
int guess=0;
cout << "请玩家猜测数字" << endl;
cin >> guess;
while(guess!=num)
{
if(guess>num)
cout << "数字过大" << endl;
else
cout << "数字过小" << endl;
cout << "请玩家猜测数字" << endl;
cin >> guess;
}
cout << "恭喜您猜对啦!" << endl;
//网课老师的方法
//srand((unsigned int)time(NULL));
//int num = rand()%100+1;
//int guess=0;
//cout << "请玩家猜测数字" << endl;
//while(1)
//{ cin >> guess;
// if(guess > num)
// cout << "数字过大" << endl;
// else if (guess < num)
// cout << "数字过小" << endl;
// else
// {
// cout << "恭喜您猜对啦!" << endl;
// break;
// }
//}
2.do…while循环语句
do{ 循环语句 } while{ 循环条件 } |
- 与while区别在于:do…while先执行一次循环语句,再判断循环条件
练习案例:水仙花数
#include<iostream>
using namespace std;
//水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身。
//如:1^3 + 5^3 + 3^3 = 153
//此题请利用 do...while语句,求出所有3位数中的水仙花数。
int main()
{
int a = 0;
int b = 0;
int c = 0;//a百位,b十位,c个位
int i = 100;
do
{
a=i/100;
b=i%100/10;
c=i%100%10;
if(a*a*a + b*b*b + c*c*c == i)
cout<<i<<endl;
i++;
}
while(i<1000);
//结果:153 370 371 407
return 0;
}
3.for循环语句
for(起始表达式;条件表达式;末尾循环体){ 循环语句; } |
注意:
- 三个表达式可以缺省,但分号不可以缺省
- 省略起始表达式,表示不对循环变量赋初值(已经赋过初值)
- 省略条件表达式,表示循环条件一直为真,若不在循环体中做终止循环处理,便成为死循环
- 省略末尾循环体,则不对循环可控制变量进行操作
案例-敲桌子
#include<iostream>
using namespace std;
//从1开始到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数
//我们打印敲桌子,其余数字直接打印输出。
int main()
{
int a = 0; //a十位,b个位。
int b = 0;
int c = 0;
for( int i = 1;i < 100;i++)
{
a = i/10;
b = i%10;
c = i%7;
if(a==7||b==7||c==0)
cout<<"敲桌子"<<endl;
else
cout<<i<<endl;
}
}
4.嵌套循环(多重循环)
案例1:*
#include<iostream>
using namespace std;
/*打印出
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
*/
int main(){
for(int i=1;i<=4;i++)
{ for(int j=1;j<=4-i;j++)
cout<<' ';
for(int t=1;t<=2*i-1;t++)
cout<<'*';
cout<<endl;
}
for(int i=1;i<4;i++)
{ for(int j=1;j<=i;j++)
cout<<' ';
for(int t=1;t<=2*(3-i)+1;t++)
cout<<'*';
cout<<endl;
}
return 0;
}
案例2:乘法口诀表
#include<iostream>
using namespace std;
//利用嵌套循环,实现九九乘法表
int main()
{
int k = 0;
for(int i = 1;i < 10;i++)
{
for(int j = 1;j < i+1;j++)
{
cout<<j<<"*"<<i<<"="<<j*i<<" ";
}
k++;
if(k%i==0)cout<<'\n';
//控制每行输出个数,可在此处直接cout<<endl;
}
}
三、跳转语句
1.break语句
(用于跳出选择结构或循环结构)
使用时机:
- 出现在switch条件语句中,作用是中止case并跳出switch
- 出现在循环语句中,作用是跳出当前循环语句
- 出现在嵌套循环中,作用是跳出最近的内层循环语句
2.continue语句
(在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环)
3.goto语句
(可以无条件跳转语句)
goto 标记;
如果标记的名称存在,执行到goto语句时,会跳转到标记的位置