Cprogramming 简明教程
C - Nested If Statements
在 C programming 中嵌套 if-else 语句总是合法的,这意味着你可以在另一个 if 或 else-if statement(s) 语句中使用一个 if 或 else-if 语句。
It is always legal in C programming to nest if-else statements, which means you can use one if or else-if statement inside another if or else-if statement(s).
在编程语境中,术语 "嵌套" 指将一个特定的编程元素包裹在另一个类似元素中。例如,嵌套循环、嵌套结构、嵌套条件语句等。如果在另一个 if 语句中使用了 if statement in C ,那么在 C 中我们称之为 nested if statement 。
In the programming context, the term "nesting" refers to enclosing a particular programming element inside another similar element. For example, nested loops, nested structures, nested conditional statements, etc. If an if statement in C is employed inside another if statement, then we call it as a nested if statement in C.
Syntax
nested if 语句的语法如下 −
The syntax of nested if statements is as follows −
if (expr1){
if (expr2){
block to be executed when
expr1 and expr2 are true
}
else{
block to be executed when
expr1 is true but expr2 is false
}
}
以下流程图表示 if 语句的嵌套 −
The following flowchart represents the nesting of if statements −
你可以将布尔表达式与 && 或 || 复合,以获得相同的效果。不过,对于有多种复合布尔表达式的复杂算法,很难形成复合条件。相反,建议使用嵌套结构。
You can compound the Boolean expressions with && or || to get the same effect. However, for more complex algorithms, where there are different combinations of multiple Boolean expressions, it becomes difficult to form the compound conditions. Instead, it is recommended to use nested structures.
另一个 if 语句可以出现在顶层 if 块中,或其 else 块中,或同时出现在两者中。
Another if statement can appear inside a top-level if block, or its else block, or inside both.
Example 1
让我们举一个例子,其中程序需要确定给定数字是否小于 100、在 100 到 200 之间、或大于 200。我们可以用以下复合布尔表达式表示此逻辑 −
Let us take an example, where the program needs to determine if a given number is less than 100, between 100 to 200, or above 200. We can express this logic with the following compound Boolean expression −
#include <stdio.h>
int main (){
// local variable definition
int a = 274;
printf("Value of a is : %d\n", a);
if (a < 100){
printf("Value of a is less than 100\n");
}
if (a >= 100 && a < 200){
printf("Value of a is between 100 and 200\n" );
}
if (a >= 200){
printf("Value of a is more than 200\n" );
}
}
Output
运行代码并检查其输出。此处,我们已将 "a" 的值初始化为 274。更改此值并重新运行代码。如果所提供的数字小于 100,那么你将得到不同的输出。类似地,如果所提供的数字在 100 和 200 之间,则输出会再次改变。
Run the code and check its output. Here, we have intialized the value of "a" as 274. Change this value and run the code again. If the supplied value is less than 100, then you will get a different output. Similarly, the output will change again if the supplied number is in between 100 and 200.
Value of a is : 274
Value of a is more than 200
Example 2
现在,针对同一问题让我们使用嵌套条件。当我们使用嵌套条件时,解决方案将更易懂。
Now let’s use nested conditions for the same problem. It will make the solution more understandable when we use nested conditions.
首先,检查“a >= 100”。在 if 语句的真部分中,检查它是否小于 200,以此来确定该数字介于 100-200 之间,或者是否大于 200。如果第一个条件(a >= 100)为 false,则表示该数字小于 100。
First, check if "a >= 100". Inside the true part of the if statement, check if it is <200 to decide if the number lies between 100-200, or it is >200. If the first condition (a >= 100) is false, it indicates that the number is less than 100.
#include <stdio.h>
int main (){
// local variable definition
// check with different values 120, 250 and 74
int a = 120;
printf("value of a is : %d\n", a );
// check the boolean condition
if(a >= 100){
// this will check if a is between 100-200
if(a < 200){
// if the condition is true, then print the following
printf("Value of a is between 100 and 200\n" );
}
else{
printf("Value of a is more than 200\n");
}
}
else{
// executed if a < 100
printf("Value of a is less than 100\n");
}
return 0;
}
Example 3
以下程序使用嵌套 if 语句来确定一个数字是否可以被 2 和 3 整除、可以被 2 但不能被 3 整除、可以被 3 但不能被 2 整除、或者既不能被 2 也不能被 3 整除。
The following program uses nested if statements to determine if a number is divisible by 2 and 3, divisible by 2 but not 3, divisible by 3 but not 2, and not divisible by both 2 and 3.
#include <stdio.h>
int main(){
int a = 15;
printf("a: %d\n", a);
if (a % 2 == 0) {
if (a % 3 == 0){
printf("Divisible by 2 and 3");
}
else {
printf("Divisible by 2 but not 3");
}
}
else {
if (a % 3 == 0){
printf("Divisible by 3 but not 2");
}
else{
printf("Not divisible by 2, not divisible by 3");
}
}
return 0;
}
Example 4
下面是检查给定年份是否是闰年的 C program 。年份是否是闰年由以下规则确定 -
Given below is a C program to check if a given year is a leap year or not. Whether the year is a leap year or not is determined by the following rules −
-
Is the year divisible by 4?
-
If yes, is it a century year (divisible by 100)?
-
If yes, is it divisible by 400? If yes, it is a leap year, otherwise not.
-
If it is divisible by 4 and not a century year, it is a leap year.
-
If it is not divisible by 4, it is not a leap year.
以下是 C 代码 -
Here is the C code −
#include <stdio.h>
int main(){
// Test the program with the values 1900, 2023, 2000, 2012
int year = 1900;
printf("year: %d\n", year);
// is divisible by 4?
if (year % 4 == 0){
// is divisible by 100?
if (year % 100 == 0){
// is divisible by 400?
if(year % 400 == 0){
printf("%d is a Leap Year\n", year);
}
else{
printf("%d is not a Leap Year\n", year);
}
}
else{
printf("%d is not a Leap Year\n", year);
}
}
else{
printf("%d is a Leap Year\n", year);
}
}
Output
运行代码并检查其输出:
Run the code and check its output −
year: 1900
1900 is not a Leap Year
用不同的值为变量“year”测试程序,例如 1900、2023、2000、2012。
Test the program with different values for the variable "year" such as 1900, 2023, 2000, 2012.
通过使用复合布尔表达式(而不是嵌套 if 语句)可以实现相同的结果,如下所示 -
The same result can be achieved by using the compound Boolean expressions instead of nested if statements, as shown below −
If (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)){
printf("%d is a leap year", year);
}
else{
printf("%d is not a leap year", year);
}
通过在 C 中使用 nested if 语句,我们可以编写结构化和多级决策算法。它们简化了对复杂的区分逻辑情况进行编码。嵌套还使得程序更具可读性,且易于理解。
With nested if statements in C, we can write structured and multi-level decision-making algorithms. They simplify coding the complex discriminatory logical situations. Nesting too makes the program more readable, and easy to understand.