In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what C++ control structure means, has certain reference value, interested friends can refer to, I hope you have a lot of gains after reading this article, let Xiaobian take you to understand.
C++的控制结构和其它编程语言类似,共包含以下三种:
顺序结构
选择结构
循环结构
不知道是否有论文证明过,上述三种结构是否实现所有的逻辑。
1、顺序结构
即表达式按照上下顺序执行,比如下面的代码:
printf("Hello");printf("\n");printf("World");
程序会依次输出"Hello World"。
2、选择结构
选择结构可以使用if语句或者switch语句实现,下面分别记录。
2.1、 if语句
采用if实现的选择结构含有3种情况:
单层选择if-else
嵌套判断if-else
多重判断if-else if
单层选择的选择采用if-else实现,其语法如下:
if (表达式) 语句1
else 语句2
比如判断x是否大于10,若是则输出yes,否则输出no:
int x = 100;if (x>10) { printf("yes"); }else { printf("no"); }
此外,if-else内部可以嵌套新的判断,比如判断if x>10以后可以继续判断x和20的关系,else 后可以判断x继续判断x和5的关系:
int x = 100; if (x>10) { if (x>20) { printf("x>20"); } } else { if (x50) { printf("x>50"); }else if (x>30) { printf("x>30"); }else { printf("all no"); }2.2 、switch语句
当需要判断的条件是同一个表达式的值时可以使用switch语句,比如判断x具体为什么值:
int x = 100; switch (x) {case 10: { printf("x= 10"); break; } case 100: { printf("x = 100"); break; } default: { printf("unknown"); }}
switch语句使用有两个注意事项,第一是case中使用break才能保证后面的case不被执行;第二是case后的表达式是必须是常量表达式,比如整型、字符型或者枚举型。
3、循环结构
C++中包含3种循环语句:while、do while以及for,下面分别介绍。
3.1 、while语句
while语句表示当满足某个条件时,语句被循环执行,一般需要在循环体内部改变表达式的值,语法如下:
while (表达式) 语句
比如当x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.