目录
一、if 语句
在 C 语言中也经常需要对一些条件做出判断,从而决定执行哪一段代码,这时就需要使用选择结构语句。
if 条件语句有三种语法格式。
1、if 单分支结构
- 流程图:
- 实例:
输入一个学生的成绩 score ,判断是否及格(≥60,及格)。
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
SetConsoleOutputCP(CP_UTF8);//设置编码
int score;
cout << "请输入你的成绩吧……:";
cin>>score;
if(score>=60)
cout<<"及格"<<endl;
return 0;
}
运行如下:
2、if…else 双分支结构
- 流程图
- 实例:
输入一个学生的成绩score,判断是及格还是不及格(<60不及格,≥60,及格)。
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
SetConsoleOutputCP(CP_UTF8);//设置编码
int score;
cout << "请输入你的成绩……看看是否及格呢:";
cin>>score;
if(score>=60)
cout<<"及格"<<endl;
else
cout<<"不及格"<<endl;
return 0;
}
运行输出如下:
3、if 语句的嵌套
- 流程图
- 实例1
输入一个学生的成绩score,判断其成绩等级(<60不及格,60-69及格,70-79中等,80-89良好,90-100优秀。)。
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
/**
输入一个学生的成绩score,判断其成绩等级
**/
SetConsoleOutputCP(CP_UTF8);//设置编码
int score;
cout << "请输入你的成绩……看看等级吧:";
cin>>score;
if(score>=70)
if(score<80)
cout<<"中等"<<endl;
else if(score<90)
cout<<"良好"<<endl;
else
cout<<"优秀"<<endl;
else if(score>=60)
cout<<"及格"<<endl;
else
cout<<"不及格"<<endl;
return 0;
}
输出如下:
- 实例2:
输入一个年份,判断其是闰年还是平年(非整百年:能被4整除的为闰年;整百年:能被400整除的是闰年。)
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
/**
输入一个年份,判断其是闰年还是平年
**/
SetConsoleOutputCP(CP_UTF8);//设置编码
int year;
cout<< "请输入一个年份参数:";
cin>>year;
if((year%4==0&&year%100!=0)||year%400==0){
cout<<year<<"是个闰年"<<endl;
}else{
cout<<year<<"是个平年"<<endl;
}
return 0;
}
运行如下:
- 实例3:
输入一个整数,判断其是否为水仙花数。
(所谓的水仙花数是指一个n位数(n ≥ 3),它的每个位上的数字 n 次幂之和等于本身。
例如,3 位数 153 是水仙花,各位数字的立方和++=153。 )。
代码:
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
/**
判断一个数是否为水仙花数
**/
SetConsoleOutputCP(CP_UTF8);//设置编码
int num,a,b,c;
cout<< "请输入一个三位数:";
cin>>num;
a=num%10;
b=(num/10)%10;
c=num/100;
if(num==(a*a*a+b*b*b+c*c*c)){
cout<<num<<"是水仙花数"<<endl;
}else{
cout<<num<<"不是水仙花数"<<endl;
}
return 0;
}
运行结果:
补充:水仙花数又称阿姆斯特朗数。
三位的水仙花数共有4个:153,370,371,407;
四位的四叶玫瑰数共有3个:1634,8208,9474;
五位的五角星数共有3个:54748,92727,93084;
六位的六合数只有1个:548834;
七位的北斗七星数共有4个:1741725,4210818,9800817,9926315;
八位的八仙花数共有3个:24678050,24678051,88593477
二、switch 语句
switch 条件语句也是一种常用的选择语句,和 if 条件语句不同,它只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码。
- 流程图
- 实例:
输入一个月份,判断属于什么季节(阳历3~5月为春季,6~8月为夏季,9~11月为秋季,12月~来年2月为冬季)。
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
/**
输入一个月份,判断属于什么季节(阳历3~5月为春季,6~8月为夏季,9~11月为秋季,12月~来年2月为冬季)
**/
SetConsoleOutputCP(CP_UTF8);//设置编码
int month,season=0;//定义月份和季节变量
cout<< "请输入一个月份:";
cin>>month;
if(month>=3&&month<=5){
season = 1;
}else if(month>=6&&month<=8){
season = 2;
}else if(month>=9&&month<=11){
season = 3;
}else if((month>=1&&month<=2)||month==12){
season = 4;
}
switch(season){
case 1:
cout<<"春季"<<endl;
break;
case 2:
cout<<"夏季"<<endl;
break;
case 3:
cout<<"秋季"<<endl;
break;
case 4:
cout<<"冬季"<<endl;
break;
default:
cout<<"您的输入有误,无法判断!"<<endl;
}
return 0;
}
运行如下:
三、关系运算与逻辑运算
运算符类型 |
作用 |
算术运算符 |
用于处理四则运算 |
赋值运算符 |
用于将表达式的值赋给变量 |
关系运算符 |
用于表达式的比较,并返回一个真值或假值 |
逻辑运算符 |
用于根据表达式的值返回真值或假值 |
三目运算符 |
用于根据表达式的值执行相应的语句 |
逗号运算符 |
用于连接并执行若干表达式,并返回最后一个表达式的值 |
位运算符 |
用于处理数据的位运算 |
sizeof运算符 |
用于求字节数长度 |
- 关系运算符
关系运算符用于对两个数值或变量进行比较,其结果是一个逻辑值(“真”或“假”)。
运算符 |
运算 |
范例 |
结果 |
== |
相等于 |
4 == 3 |
0 |
!= |
不等于 |
4 != 3 |
1 |
< |
小于 |
4 < 3 |
0 |
> |
大于 |
4 > 3 |
1 |
<= |
小于等于 |
4 <= 3 |
0 |
>= |
大于等于 |
4 >= 3 |
1 |
- 逻辑运算符
逻辑运算符用于判断数据的真假,其结果仍为“真”或“假”。
运算符 | 运算 | 范例 | 结果 |
---|---|---|---|
! | 非 | !a | 如果a为假,则!a为真 如果a为真,则!a为假 |
&& | 与 | a&&b | 如果a和b都为真,则结果为真否则为假 |
|| | 或 | a || b | 如果a和b有一个或一个以上为真,则结果为真,二者都为假,结果为假 |