当前位置: 首页 > article >正文

NO.11十六届蓝桥杯备战|if-else语句|嵌套if|悬空else|练习4道(C++)

if-else语句

if语句

if语句的语法形式如下:

if ( 表达式 )  
	语句;

表达式成⽴(为真),则语句执⾏,表达式不成⽴(为假),则语句不执⾏
0为假,⾮0表⽰真,也就是表达式的结果如果是 0 ,则语句不执⾏,表达式的结果如果是不是 0 ,则语句执⾏。

题目
输⼊⼀个正整数n,判断n是否为奇数
![[Pasted image 20250210174223.png]]

#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;  
}

http://www.kler.cn/a/541145.html

相关文章:

  • 渗透利器工具:Burp Suite 联动 XRAY 图形化工具.(主动扫描+被动扫描)
  • Ubuntu 24.10 安装Deepseek(Ollama+openwebui)
  • CTFHub-RCE系列wp
  • Linux磁盘空间使用率100%(解决删除文件后还是显示100%)
  • Avnet RFSoC基于maltab得5G 毫米波 开发工具箱
  • 【人工智能】解码语言之谜:使用Python构建神经机器翻译系统
  • AWS在游戏开发与全球发行中的技术实践与架构解析
  • lxml 教程:Python 中高效处理 XML 和 HTML 的利器
  • 牛客周赛Round 80——举手赢棋 python 补题 + 题解
  • Django视图与URLs路由详解
  • 人工智能学习(七)之神经网络
  • Spring基于文心一言API使用的大模型
  • PyTorch torch.unbind、torch.split 和 torch.chunk函数介绍
  • 在 C# 中,处理 Excel 和 PDF 文件的库有很多。以下是一些比较常用的选择
  • Unity使用新输入系统控制物体移动
  • VUE项目中实现权限控制,菜单权限,按钮权限,接口权限,路由权限,操作权限,数据权限实现
  • 基于yolo的视频检测分析
  • Linux网络编程--Udp套接字+实战 (万字详解,超详细!!)
  • 【多线程-第三天-NSOperation和GCD的区别 Objective-C语言】
  • 游戏启动不了了?一步步解决kaeon.dll丢失故障
  • VSCode + Continue 实现AI编程助理
  • 四、OSG学习笔记-基础图元
  • 【前端基础】深度理解JavaScript中的异步机制
  • React(三)
  • 每日一题——插入排序实现数据流中的中位数
  • 【Java】多线程和高并发编程(四):阻塞队列(上)基础概念、ArrayBlockingQueue