当前位置:网站首页>C语言日记 9 if的3种语句
C语言日记 9 if的3种语句
2022-08-05 02:08:00 【宇 -Yu】
if
一. if语句
书P39例3-3:
#include <iostream>
using namespace std;
void main()
{
int a, b, max;
cout << "Please input two numbers: " << endl;
cin >> a >> b;
max = a;
if (max < b) max = b;
cout << "max=" << max << endl;
}
本project遇到的问题:
1.必须写成“if(<表达式>)语句”的形式吗?
if <表达式>
语句
的形式也可以,例:
#include <iostream>
using namespace std;
void main()
{
int a, b, max;
cout << "Please input two numbers: " << endl;
cin >> a >> b;
max = a;
if (max < b)
max = b;
cout << "max=" << max << endl;
}
2.cin输入时,保持输入>>,不要中途一般突然变成输入“,”
这样就会报错:变量未初始化。例:
#include <iostream>
using namespace std;
int main()
{
int a, b, max;
cout << "Please input two numbers: " << endl;
cin >> a , b;
max = a;
if (max < b) max = b;
cout << "max=" << max << endl;
}

3.cin输入时,输入多个变量,用“enter(换行键)”输入区分不同变量,而不是用“,”来区分,例:

此时无论变量b输入什么值,结果都只输出a
(因为使用“,”后默认忽略“,”后的b或默认“,”后为0)
4.VISUAL BASIC中的“if...then...”语句在C++中不成立
(if语句后面不能输入then),例:
#include <iostream>
using namespace std;
int main()
{
int a, b, max;
cout << "Please input two numbers: " << endl;
cin >> a >> b;
max = a;
if (max < b) then max = b;
cout << "max=" << max << endl;
}

二. if...else语句
书P39例3-4:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please input two numbers:" << endl;
cin >> a >> b;
if (a > b)
cout << "max=" << a << endl;
else
cout << "max=" << b << endl;
}
三. if...else...if语句
书P40例3-5:
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "input a character: " << endl;
cin >> c;
if (c < 32)
cout << "This is a control character." <<endl;
else if (c >= '0' && c <= '9')
cout << "This is a digit." <<endl;
else if (c >= 'A' && c <= 'Z')
cout << "This is an upper case letter." << endl;
else if (c >= 'a' && c <= 'z')
cout << "This is a lower case letter." << endl;
else
cout << "This is an other character." << endl;
}
注意:
输入字符串一样当作输出大写/小写字母来处理,例:

个人认为,这个 if...else...if语句,其实本质上来说并不是 if...else...if,而是 if...else if
因为 if...else...if 语句的效果(如下与书P40)所绘的流程图并不相同:
例:
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a ;
if (a==1)
cout << 1<< endl;
else
cout<< 2 << endl;
if (a == 2)
cout <<3 << endl;
}
结果:

所以应该是:
if...else if语句
这里我们讨论的都是C++中函数函数内还不含”{ }“的情况,
C语言中的一些规则以及内部使用”{ }“的情况,详情可参考:
C语言:if、if...else、if...else if ...else、if...if...if 语句的区别_斯文~的博客-CSDN博客_if if
边栏推荐
- 汇编语言之源程序
- 用@Mapper查询oracle的分区情况报错
- source program in assembly language
- Fragment visibility judgment
- 第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
- Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
- .Net C# 控制台 使用 Win32 API 创建一个窗口
- 进程在用户态和内核态的区别[独家解析]
- Residential water problems
- 原生js实现多选框全部选中和取消效果
猜你喜欢

<开发>实用工具

【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗

如何基于OpenVINO POT工具简单实现对模型的量化压缩

编译预处理等细节

CPDA|运营人如何从负基础学会数据分析(SQL)

LPQ(局部相位量化)学习笔记

Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion

程序员失眠时的数羊列表 | 每日趣闻
![[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?](/img/eb/535ffaff9b535fbc73a4d56aab0b3a.png)
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?

Xunrui cms website cannot be displayed normally after relocation and server change
随机推荐
.Net C# Console Create a window using Win32 API
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
意识形态的机制
PHP技能评测
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
为什么他们选择和AI恋爱?
转:查尔斯·汉迪:你是谁,比你做什么更重要
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
蚁剑高级模块开发
力扣-二叉树的最大的深度
短域名绕过及xss相关知识
使用OpenVINO实现飞桨版PGNet推理程序
Xunrui cms website cannot be displayed normally after relocation and server change
[Machine Learning] 21-day Challenge Study Notes (2)
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
Utilities iNFTnews | What can NFTs bring to the sports industry and fans?
在这个超连接的世界里,你的数据安全吗
迁移学习——Distant Domain Transfer Learning