NO.11十六届蓝桥杯备战|if-else语句|嵌套if|悬空else|练习4道(C++)
if-else语句
if语句
if语句的语法形式如下:
if ( 表达式 )
语句;
表达式成⽴(为真),则语句执⾏,表达式不成⽴(为假),则语句不执⾏
0为假,⾮0表⽰真,也就是表达式的结果如果是 0 ,则语句不执⾏,表达式的结果如果是不是 0 ,则语句执⾏。
题目
输⼊⼀个正整数n,判断n是否为奇数
#include <iostream>
using namespace std;
int n;
int main()
{
cin >> n;
if (n % 2 == 1)
cout << n << "是奇数" << endl;
return 0;
}
else语句
语法形式如下:
if ( 表达式 )
语句1;
else
语句2;
题⽬:
输⼊⼀个正整数 n ,如果 n 是奇数就打印奇数 ,是偶数就打印偶数 。
#include <iostream>
using namespace std;
int n;
int main()
{
cin >> n;
if (n % 2 == 1)
cout << n << "是奇数" << endl;
else
cout << n << "是偶数" << endl;
return 0;
}
练习:
输⼊⼀个数表⽰年龄,年龄 >= 18 岁就输出: 成年 ,否则就输出: 未成年
#include <iostream>
using namespace std;
int age;
int main()
{
cin >> age;
if (age >= 18)
cout << "成年" << endl;
else
cout << "未成年" << endl;
return 0;
}
如果 age>=18 的时候,还需要打印: 可以谈恋爱了 , age < 18 时,还需要提⽰ “不能早恋” ,这时候 if 和 else 控制的就是2条语句了,就得带上 {} 。
#include <iostream>
using namespace std;
int age;
int main()
{
cin >> age;
if (age >= 18)
{
cout << "成年" << endl;
cout << "可以谈恋爱了" << endl;
}
else
{
cout << "未成年" << endl;
cout << "不能早恋" << endl;
}
return 0;
}
if / else 后边默认只能控制⼀条语句,如果需要写多条语句的时候,需要使⽤ {}
嵌套if
在 if else 语句中, else 可以与另⼀个 if 语句连⽤,构成多重判断。
//形式1
if()
语句1;
else
{
if()
语句2;
else
语句3;
}
//形式2
if()
语句1;
else if()
语句2;
else
语句3;
其中的部分相当于嵌套在 else 语句中; if…else 语句是在 if 或者 else 中选择执⾏其中⼀句代码,其实 if…else 是⼀条语句,那么 形式 1 中代码外边的 {} 就可以省略掉,就和 形式 2 ⼀样了。
B2035 判断数正负
#include <iostream>
using namespace std;
int n;
int main()
{
cin >> n;
if (n > 0)
cout << "positive" << endl;
else if (n == 0)
cout << "zero" << endl;
else
cout << "negative" << endl;
return 0;
}
int main()
{
cin >> n;
if (n >= 0)
{
if (n > 0)
cout << "positive" << endl;
else if (n == 0)
cout << "zero" << endl;
}
else
cout << "negative" << endl;
return 0;
}
悬空else
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 2;
if(a == 1)
if(b == 2)
cout << "hehe" << endl;
else
cout << "haha" << endl;
return 0;
}
如果有多个 if 和 else ,可以记住这样⼀条规则, else 总是跟最接近的 if 匹配
实际上 else 是和第⼆个 if 进⾏匹配的,这样后边的 if…else 语句是嵌套在第⼀个 if 语句中的,如果第⼀个 if 语句就不成⽴,嵌套 if 和 else 就没机会执⾏了,最终啥都不打印。
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 2;
if(a == 1)
{
if(b == 2)
cout << "hehe" << endl;
else
cout << "haha" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 2;
if(a == 1)
{
if(b == 2)
cout << "hehe" << endl;
}
else
{
cout << "haha" << endl;
}
return 0;
}
只要带上适当的⼤括号,代码的逻辑就会更加的清晰,所以⼤家以后在写代码的时候要注意括号的使⽤,让代码的可读性更⾼。
练习
整除判断
#include <iostream>
using namespace std;
int m, n;
int main()
{
cin >> m >> n;
if (m % n == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
- 判断相等是 == ,不能写成 = 。
- 注意输出信息的格式(⼤⼩写)。
B2039 整数大小比较
#include <iostream>
using namespace std;
long long x, y;
int main()
{
cin >> x >> y;
if (x > y)
cout << ">" << endl;
else if (x == y)
cout << "=" << endl;
else
cout << "<" << endl;
return 0;
}
int 类型的取值范围是: -231~231-1 ,即使是 unsigned int 的取值范围也就是: 0~2^32-1 ,那么题⽬中的 x, y 使⽤ int 类型就不合适了,应该使⽤ long long 类型。
B2036 输出绝对值
#include <iostream>
#include <cstdio>
using namespace std;
double n;
int main()
{
cin >> n;
if (n <= 0)
printf("%.2f\n", -n);
else
printf("%.2f\n", n);
return 0;
}
B2036 输出绝对值
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float n = 0;
scanf("%f", &n);
n = fabs(n);
printf("%.2f\n", n);
return 0;
}
这⾥使⽤了⼀个库函数 fabs 是⽤来求⼀个浮点数的绝对值的,需要的头⽂件是 <cmath>
。
函数原型如下:
double fabs (double x);
float fabs (float x);
long double fabs (long double x);
相关的函数还有 abs ,是求整数的绝对值的,需要的头⽂件是 <cstdlib>
。
函数原型如下:
int abs (int n);
long int abs (long int n);
B2037 奇偶数判断
#include <iostream>
using namespace std;
int n;
int main()
{
cin >> n;
if (n % 2 == 1)
cout << "odd" << endl;
else if (n % 2 == -1)
cout << "odd" << endl;
else
cout << "even" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cin >> n;
if (n % 2)
cout << "odd" << endl;
else
cout << "even" << endl;
return 0;
}