当前位置:网站首页>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
边栏推荐
- 进程在用户态和内核态的区别[独家解析]
- 如何逐步执行数据风险评估
- ".NET IoT from scratch" series
- 力扣-二叉树的最大的深度
- Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
- hypervisor相关的知识点
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- 力扣-相同的树
- LPQ (local phase quantization) study notes
- Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
猜你喜欢

树形查找(二叉查找树)

树表的查找

多线程(2)

编译预处理等细节

MySQL3

Using OpenVINO to implement the flying paddle version of the PGNet inference program

Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
![[Endnote] Word inserts a custom form of Endnote document format](/img/70/e8a6f15b80e9c53db369fd715e51df.png)
[Endnote] Word inserts a custom form of Endnote document format

KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)

如何逐步执行数据风险评估
随机推荐
ARM Mailbox
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
亚马逊云科技携手中科创达为行业客户构建AIoT平台
ExcelPatternTool: Excel表格-数据库互导工具
C语言日记 9 if的3种语句
PHP技能评测
1349. 参加考试的最大学生数 状态压缩
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
CMS website construction process
SDC简介
学习笔记-----左偏树
Using OpenVINO to implement the flying paddle version of the PGNet inference program
【日常训练】1403. 非递增顺序的最小子序列
LPQ (local phase quantization) study notes
力扣-二叉树的前序遍历、中序遍历、后序遍历
树表的查找
1349. Maximum number of students taking the exam Status Compression
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
力扣-二叉树的最大的深度
“配置”是把双刃剑,带你了解各种配置方法