How is the if else statement used in the C programming language?

In the C language, the if-else statement is used to execute different code blocks based on the condition being true or false. Its syntax is as follows:

if (条件) {
    // 如果条件为真,执行这里的代码块
} else {
    // 如果条件为假,执行这里的代码块
}

If the condition is true, the code block inside the if statement is executed; if the condition is false, the code block inside the else statement is executed. The else statement is optional and can be omitted.

Here is a simple example demonstrating the usage of the if else statement:

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("num是正数\n");
    } else {
        printf("num是负数\n");
    }

    return 0;
}

The output is: num is a positive number.

bannerAds