在C编程中,嵌套if-else语句始终是合法的,这意味着您可以在另一个if或else if语句中使用一个if或else if语句。
嵌套if语句的语法如下所示:-
链接:https://www.learnfk.comhttps://www.learnfk.com/c-programming/nested-if-statements-in-c.html
来源:LearnFk无涯教程网
if( boolean_expression 1) { /* 当布尔表达式 1 为真时执行*/ if(boolean_expression 2) { /* 当布尔表达式 2 为真时执行 */ } }
您可以使用与嵌套if语句类似的方式嵌套else if.else。
#include <stdio.h> int main () { /* 局部变量定义 */ int a = 100; int b = 200; /* 检查布尔条件 */ if( a == 100 ) { /* 如果条件为真,则检查以下内容 */ if( b == 200 ) { /* 如果条件为真,则打印以下内容 */ printf("Value of a is 100 and b is 200\n" ); } } printf("Exact value of a is : %d\n", a ); printf("Exact value of b is : %d\n", b ); return 0; }
编译并执行上述代码后,将产生以下结果-
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)