当前位置:网站首页>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
边栏推荐
- 2022 EdgeX中国挑战赛8月3日即将盛大开幕
- C学生管理系统 头添加学生节点
- Domain Driven Design - MDD
- Residential water problems
- ExcelPatternTool: Excel table-database mutual import tool
- 力扣-相同的树
- Leetcode brushing questions - 22. Bracket generation
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- C学生管理系统 指定位置插入学生节点
- Programmer's list of sheep counting when insomnia | Daily anecdote
猜你喜欢
MySQL学习
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
Using OpenVINO to implement the flying paddle version of the PGNet inference program
迁移学习——Distant Domain Transfer Learning
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
Three handshake and four wave in tcp
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
【MySQL series】- Does LIKE query start with % will make the index invalid?
随机推荐
fragment可见性判断
蚁剑高级模块开发
原生js实现多选框全部选中和取消效果
编译预处理等细节
2022杭电多校第一场
[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?
.Net C# 控制台 使用 Win32 API 创建一个窗口
Pisanix v0.2.0 发布|新增动态读写分离支持
Short domain name bypass and xss related knowledge
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
迅睿cms网站搬迁换了服务器后网站不能正常显示
树表的查找
Programmer's list of sheep counting when insomnia | Daily anecdote
程序员失眠时的数羊列表 | 每日趣闻
【PyQT5 绑定函数的传参】
【日常训练】1403. 非递增顺序的最小子序列
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
【Endnote】Word插入自定义形式的Endnote文献格式
[Machine Learning] 21-day Challenge Study Notes (2)
力扣-二叉树的前序遍历、中序遍历、后序遍历