C/C++ 语言中的 if...else if...else 语句

1. if statement

The syntax of the if statement is:

if (condition) {
    // body of if statement
}

The code inside { } is the body of the if statement.

在这里插入图片描述

If the condition evaluates to true, the code inside the body of if is executed.

If the condition evaluates to false, the code inside the body of if is skipped.

2. if...else statement

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

if(boolean_expression) {
    // statement(s) will execute if the boolean expression is true
} else {
    // statement(s) will execute if the boolean expression is false
}

在这里插入图片描述

在这里插入图片描述

If the condition evaluates true,

the code inside the body of if is executed
the code inside the body of else is skipped from execution

If the condition evaluates false,

the code inside the body of else is executed
the code inside the body of if is skipped from execution

3. if...else if...else statement

The syntax of the if...else if...else statement is:

if (condition1) {
    // code block 1
}
else if (condition2){
    // code block 2
}
else {
    // code block 3
}

If condition1 evaluates to true, the code block 1 is executed.

If condition1 evaluates to false, then condition2 is evaluated.

If condition2 is true, the code block 2 is executed.

If condition2 is false, the code block 3 is executed.

在这里插入图片描述

if (condition1) {
    // block of code to be executed if condition1 is true
} else if (condition2) {
    // block of code to be executed if the condition1 is false and condition2 is true
} else {
    // block of code to be executed if the condition1 is false and condition2 is false
}

在这里插入图片描述

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] C++ if, if…else and Nested if…else, https://www.programiz.com/cpp-programming/if-else

相关推荐

  1. gitlab-ci_cd语法CICD

    2024-03-27 06:46:04       21 阅读
  2. C语言switch语句case后()

    2024-03-27 06:46:04       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 06:46:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 06:46:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 06:46:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 06:46:04       20 阅读

热门阅读

  1. AI绘画自动生成器平台有哪些

    2024-03-27 06:46:04       17 阅读
  2. python数据解析xpath

    2024-03-27 06:46:04       18 阅读
  3. 微信小程序 第四节课

    2024-03-27 06:46:04       18 阅读
  4. pytorch中的torch.hub.load()

    2024-03-27 06:46:04       21 阅读
  5. 09 mybatis 注解

    2024-03-27 06:46:04       17 阅读
  6. PgMP考试费用是多少?收费标准详细解析!

    2024-03-27 06:46:04       35 阅读
  7. 1969. 数组元素的最小非零乘积

    2024-03-27 06:46:04       18 阅读
  8. QT学习之UDP

    2024-03-27 06:46:04       17 阅读
  9. spring缓存通用配置

    2024-03-27 06:46:04       18 阅读
  10. sqlite删除数据表

    2024-03-27 06:46:04       18 阅读
  11. GPT大语言模型助力R语言开展数据统计分析

    2024-03-27 06:46:04       13 阅读
  12. torchvision.datasets.ImageFolder

    2024-03-27 06:46:04       15 阅读
  13. 在虚拟机CentOs_7_64环境中安装Docker和Docker-Compose

    2024-03-27 06:46:04       15 阅读
  14. 如何利用nginx在Centos上搭建文件服务器

    2024-03-27 06:46:04       22 阅读