当前位置:网站首页>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
边栏推荐
- “嘀哩哩,等灯等灯”,工厂安全生产的提示音
- Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
- 居民用水问题
- 树形查找(二叉查找树)
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
- Opening - Open a new .NET modern application development experience
- 如何模拟后台API调用场景,很细!
- Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
- 学习笔记-----左偏树
- 特殊矩阵的压缩存储
猜你喜欢
![[Endnote] Word inserts a custom form of Endnote document format](/img/70/e8a6f15b80e9c53db369fd715e51df.png)
[Endnote] Word inserts a custom form of Endnote document format

iNFTnews | What can NFTs bring to the sports industry and fans?

使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号

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

ExcelPatternTool: Excel表格-数据库互导工具

.Net C# 控制台 使用 Win32 API 创建一个窗口

HOG特征学习笔记

Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"

02 【开发服务器 资源模块】

Using OpenVINO to implement the flying paddle version of the PGNet inference program
随机推荐
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
".NET IoT from scratch" series
How do programmers without objects spend the Chinese Valentine's Day
source program in assembly language
重新审视分布式系统:永远不会有完美的一致性方案……
第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
没有对象的程序员如何过七夕
进程在用户态和内核态的区别[独家解析]
CMS建站流程
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
【PyQT5 绑定函数的传参】
优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
力扣-二叉树的前序遍历、中序遍历、后序遍历
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
如何创建rpm包
C学生管理系统 指定位置插入学生节点
MySQL学习
MySQL3