当前位置:网站首页>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
边栏推荐
- Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
- 关于#sql shell#的问题,如何解决?
- 第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
- 为什么他们选择和AI恋爱?
- Why is this problem reported when installing oracle11
- [parameters of PyQT5 binding functions]
- PHP Skills Assessment
- 记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
- How to create an rpm package
- AI+PROTAC|dx/tx完成500万美元种子轮融资
猜你喜欢

一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征

sql语句多字段多个值如何进行排序

【Word】Word公式导出PDF后出现井号括号#()错误

使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据

"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory

MySQL learning

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

Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit

直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning

Day Fourteen & Postman
随机推荐
特殊矩阵的压缩存储
EBS uses virtual columns and hint hints to optimize sql case
浅谈数据安全治理与隐私计算
IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
亚马逊云科技 + 英特尔 + 中科创达为行业客户构建 AIoT 平台
C学生管理系统 头添加学生节点
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
Leetcode刷题——22. 括号生成
.Net C# 控制台 使用 Win32 API 创建一个窗口
Pisanix v0.2.0 发布|新增动态读写分离支持
".NET IoT from scratch" series
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
进程在用户态和内核态的区别[独家解析]
力扣-二叉树的前序遍历、中序遍历、后序遍历
开篇-开启全新的.NET现代应用开发体验
“配置”是把双刃剑,带你了解各种配置方法
C学生管理系统 指定位置插入学生节点
GC高德坐标和百度坐标转换
如何基于OpenVINO POT工具简单实现对模型的量化压缩
Object.defineProperty实时监听数据变化并更新页面