当前位置:网站首页>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
边栏推荐
- Greenplum数据库故障分析——版本升级后gpstart -a为何返回失败
- 使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
- Programmer's list of sheep counting when insomnia | Daily anecdote
- Transfer Learning - Distant Domain Transfer Learning
- 重新审视分布式系统:永远不会有完美的一致性方案……
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
Utilities - IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
- The difference between a process in user mode and kernel mode [exclusive analysis]
- 树表的查找
猜你喜欢

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

释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展

js中try...catch和finally的用法

特殊矩阵的压缩存储

海量服务实例动态化管理

直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计

J9数字货币论:web3的创作者经济是什么?

【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗

Method Overriding and Object Class

Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
随机推荐
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
Programmer's list of sheep counting when insomnia | Daily anecdote
the mechanism of ideology
XMjs跨域问题解决
source program in assembly language
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
C学生管理系统 指定位置插入学生节点
1349. Maximum number of students taking the exam Status Compression
leetcode-对称二叉树
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
转:查尔斯·汉迪:你是谁,比你做什么更重要
sql语句多字段多个值如何进行排序
2022杭电多校第一场
LPQ (local phase quantization) study notes
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
用@Mapper查询oracle的分区情况报错
C language basics -- pointers
力扣-二叉树的前序遍历、中序遍历、后序遍历
使用OpenVINO实现飞桨版PGNet推理程序
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?