当前位置:网站首页>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
边栏推荐
- ExcelPatternTool: Excel表格-数据库互导工具
- 力扣-相同的树
- iNFTnews | What can NFTs bring to the sports industry and fans?
- EBS利用虚拟列及hint 提示优化sql案例一则
- Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
- DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
- 基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
- 迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- Leetcode brushing questions - 22. Bracket generation
- 浅谈数据安全治理与隐私计算
猜你喜欢
随机推荐
1349. Maximum number of students taking the exam Status Compression
CPDA|运营人如何从负基础学会数据分析(SQL)
C学生管理系统 指定位置插入学生节点
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
程序员失眠时的数羊列表 | 每日趣闻
【Endnote】Word插入自定义形式的Endnote文献格式
优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
【PyQT5 绑定函数的传参】
MySQL3
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
短域名绕过及xss相关知识
[Endnote] Word inserts a custom form of Endnote document format
重新审视分布式系统:永远不会有完美的一致性方案……
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
SuperMap支持的国产环境汇总
树表的查找
学习笔记-----左偏树
CMS建站流程