How are three consecutive “if” statements executed in the C language?

In the C language, when multiple if statements are listed in parallel, they are evaluated in the order they appear from top to bottom. Only when the condition of a specific if statement is true, will its corresponding code block be executed, and then the program will continue to execute the subsequent code after exiting the entire if statement block.

For example, the following code snippet demonstrates the parallel execution of three if statements.

int x = 10;

if (x > 5) {
    printf("x is greater than 5\n");
}

if (x < 20) {
    printf("x is less than 20\n");
}

if (x == 10) {
    printf("x is equal to 10\n");
}

The following steps will be carried out in the order listed:

  1. First, check if x is greater than 5. Since the condition is true, the output is “x is greater than 5”.
  2. Then it is determined whether x is less than 20, since the condition is true, it will output “x is less than 20”.
  3. Finally, check if x is equal to 10. Since the condition is true, the output will be “x is equal to 10”.

Therefore, the output result of the above code is:

x is greater than 5
x is less than 20
x is equal to 10
bannerAds