首页 > 编程语言 >C/C++ 语言中的 ​if...else if...else 语句

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

时间:2024-03-28 19:04:19浏览次数:19  
标签:... executed C++ else code statement block

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

标签:...,executed,C++,else,code,statement,block
From: https://blog.csdn.net/chengyq116/article/details/137060387

相关文章

  • 19、C++的指针基础
    1、指针的基本概念(1)变量的地址变量是内存变量的简称,在C++中,每定义一个变量,系统就会给变量分配一块内存,内存是有地址的。C++用运算符&获取变量在内存中的起始地址。语法:&变量名(2)指针变量指针变量简称指针,它是一种特殊的变量,专用于存放变量在内存中的起始地址。语法:数据......
  • C++_基础内容复习-跟着代码学
    二进制文件读写ios_base::out 以写入方式打开文件。ios_base::binary 以二进制模式打开文件std::ofstreamofs(FILE_PATH,ios_base::app);//以追加的形式打开文件//写入学生数量intnumStudents=students.size();ofs.write(reinterpret_cast<constcha......
  • MySQL各类查询语句DQL--like in between...and
    数据库名称可以为【schoolDB】,字符集【utf8】,排列规则【utf8_general_ci】建立表CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`createDate`datetimeDEFAULTNULL,`userName`varchar(20)DEFAULTNULL,`pwd`varchar(36)DE......
  • C++第五十七篇——RPC进程间通信
    第一步:新建一个空项目 第二步:新建一个IDL 第三步:生成一个GUID,编写RPCConn.idl RPCConn.idlimport"oaidl.idl";import"ocidl.idl";[uuid(1BA624D4-DC7D-484C-AF8C-0EF86C4A0555),version(1.0)]interfaceRPCConn{intAdd([in]inta,......
  • C++入门————第一天
    1、命名空间     在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。  1......
  • C语言例4-29:计算1+2+...+100之和(利用do-while语句实现)。
    代码如下://计算1+2+...+100之和(利用do-while语句实现)。#include<stdio.h>intmain(void){ intn=1,sum=0; do { sum=sum+n; n++; }while(n<=100); printf("sum=%d\n",sum); return0;}结果如下:说明:本例中do-while循环和while循环完成相同的功能。但是,当......
  • Linux C++ 007-指针
    LinuxC++007-指针本节关键字:Linux、C++、指针、函数指针相关库函数:基本概念指针的作用:可以通过指针间接访问内存。内存编号是从0开始记录的,一般用于十六进制数字表示,可以利用指针变量保存地址。定义和使用指针变量定义语法:数据类型*变量名;指针所占内存空间,指针......
  • Linux C++ 008-结构体
    LinuxC++008-结构体本节关键字:Linux、C++、结构体相关库函数:基本概念结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。定义和使用语法:struct结构体名{结构体成员列表};通过结构体创建变量的方式有三种struct结构体名变量名struct结构体名变......
  • C++ Primer Plus 代码学习解析(第三章 3.8-3.11)
    3.8floatnum.cpp#include<iostream>intmain(){usingnamespacestd;cout.setf(ios_base::fixed,ios_base::floatfield);floattub=10.0/3.0;doublemint=10.0/3.0;constfloatmillion=1.0e6;cout<<&......
  • C++之STL整理(2)之vector超详用法整理
    C++之STL整理(2)之vector用法(创建、赋值、方法)整理注:整理一些突然学到的C++知识,随时mark一下例如:忘记的关键字用法,新关键字,新数据结构C++的vector用法整理C++之STL整理(2)之vector用法(创建、赋值、方法)整理一、vector的初始化1、默认构造函数2、拷贝构造函数copy区间3......