当前位置:网站首页>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
边栏推荐
- Fragment visibility judgment
- nodeJs--封装路由
- 1349. Maximum number of students taking the exam Status Compression
- Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
- dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- 浅谈数据安全治理与隐私计算
- 超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
- 【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
- 如何逐步执行数据风险评估
猜你喜欢
HOG特征学习笔记
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
1349. Maximum number of students taking the exam Status Compression
Utilities 如何基于OpenVINO POT工具简单实现对模型的量化压缩
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
迅睿cms网站搬迁换了服务器后网站不能正常显示
在这个超连接的世界里,你的数据安全吗
随机推荐
常见的硬件延迟
.Net C# 控制台 使用 Win32 API 创建一个窗口
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
CPDA|运营人如何从负基础学会数据分析(SQL)
继承关系下构造方法的访问特点
Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
散列表的查找(哈希表)
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
从零到一快速学会三子棋
[Endnote] Word inserts a custom form of Endnote document format
【PyQT5 绑定函数的传参】
1349. Maximum number of students taking the exam Status Compression
LeetCode使用最小花费爬楼梯----dp问题
oracle将restful接口封装到视图中
Flink 1.15.1 集群搭建(StandaloneSession)
【MySQL series】- Does LIKE query start with % will make the index invalid?
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
2022 EdgeX中国挑战赛8月3日即将盛大开幕
PHP Skills Assessment
如何模拟后台API调用场景,很细!