当前位置:网站首页>C language diary 9 3 kinds of statements of if
C language diary 9 3 kinds of statements of if
2022-08-05 02:18:00 【Yu - 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输入时,保持输入>>,Don't suddenly become input halfway through“,”
这样就会报错:变量未初始化.例:
#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 multiple variables,用“enter(换行键)”Input distinguishes between different variables,而不是用“,”来区分,例:

At this point regardless of the variablebWhat value to enter,The results are only outputa
(因为使用“,”is ignored by default“,”后的b或默认“,”后为0)
4.VISUAL BASIC中的“if...then...”语句在C++中不成立
(ifStatements cannot be entered afterthen),例:
#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;
}
注意:
The input string is treated as uppercase as the output/lowercase letters,例:

个人认为,这个 if...else...if语句,Actually it is not if...else...if,而是 if...else if
因为 if...else...if 语句的效果(as follows with the bookP40)The flowcharts drawn are not the same:
例:
#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语句
Here we are discussingC++The function is not included in the function”{ }“的情况,
CSome rules in the language and internal use”{ }“的情况,详情可参考:
C语言:if、if...else、if...else if ...else、if...if...if 语句的区别_斯文~的博客-CSDN博客_if if
边栏推荐
- 高数_复习_第1章:函数、极限、连续
- Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
- 如何看待自己的羞愧感
- 一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
- PHP Skills Assessment
- LPQ (local phase quantization) study notes
- 浅谈数据安全治理与隐私计算
- AI+小核酸药物|Eleven完成2200万美元种子轮融资
- 记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
- 1349. Maximum number of students taking the exam Status Compression
猜你喜欢
随机推荐
如何创建rpm包
《.NET物联网从零开始》系列
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
C语言基础知识 -- 指针
迁移学习——Distant Domain Transfer Learning
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
转:查尔斯·汉迪:你是谁,比你做什么更重要
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
AI+小核酸药物|Eleven完成2200万美元种子轮融资
std::string::find 返回值的坑
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
C language basics -- pointers
MySQL3
网络安全与元宇宙:找出薄弱环节
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
1349. 参加考试的最大学生数 状态压缩
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)








