当前位置:网站首页>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
边栏推荐
猜你喜欢
Opening - Open a new .NET modern application development experience
ExcelPatternTool: Excel table-database mutual import tool
A new technical director, who calls DDD a senior, is convinced
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
Xunrui cms website cannot be displayed normally after relocation and server change
2022 EdgeX中国挑战赛8月3日即将盛大开幕
编译预处理等细节
为什么他们选择和AI恋爱?
Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
随机推荐
LPQ (local phase quantization) study notes
EBS uses virtual columns and hint hints to optimize sql case
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
oracle将restful接口封装到视图中
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
MySQL3
Hypervisor related knowledge points
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
开篇-开启全新的.NET现代应用开发体验
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
GC高德坐标和百度坐标转换
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
HOG feature study notes
hypervisor相关的知识点
PHP Skills Assessment
ExcelPatternTool: Excel table-database mutual import tool
JZ搜索引擎solr研究-从数据库创建索引
C语言基础知识 -- 指针