当前位置:网站首页>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
边栏推荐
猜你喜欢

关于#sql shell#的问题,如何解决?

The use of pytorch: temperature prediction using neural networks

IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型

ExcelPatternTool: Excel table-database mutual import tool

Day Fourteen & Postman

2022 EdgeX中国挑战赛8月3日即将盛大开幕

金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)

iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?

<开发>实用工具

程序员失眠时的数羊列表 | 每日趣闻
随机推荐
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了
没有对象的程序员如何过七夕
CMS建站流程
MySQL3
C学生管理系统 头添加学生节点
The use of pytorch: temperature prediction using neural networks
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
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
如何逐步执行数据风险评估
Domain Driven Design - MDD
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
【日常训练】1403. 非递增顺序的最小子序列
source program in assembly language
Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
EBS uses virtual columns and hint hints to optimize sql case
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
HOG特征学习笔记
.Net C# 控制台 使用 Win32 API 创建一个窗口
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?
为什么他们选择和AI恋爱?