English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C Language if...else Statements

In this tutorial, you will learn about the if statement in C language programming with the help of examples (including if ... else and nested if..else).

C Language if statement

The syntax of this statement in C language programming is:

if (test expression) 
{
   //The statements to be executed when the test expression is true
}

How does the if statement work?

The if statement evaluates the test expression inside the parentheses().

  • If the calculation result of the test expression is true, then the if will execute the statements inside the body.

  • If the calculation result of the test expression is false, the if will not execute the statements inside the body.

For more information on when to evaluate the test expression as true (non-zero value) and false (0), please checkrelationoperatorandLogical operators.

Example1:if statement

//The program displays a number if it is negative
#include <stdio.h>
int main() {
    int number;
    printf("Please enter an integer: ");
    scanf("%d", &number);
    //If the number is less than 0, it is true
    if (number < 0) {
        printf("You entered %d.\n", number);
    }
    printf("The if statement is very simple.");
    return 0;
}

Output1

Enter an integer: -25
You entered  -25.
The if statement is very simple.

when the user enters-25When, the test expression number<0 will be evaluated as true. Therefore,You entered-25 will be displayed on the screen.

Output2

Enter an integer: 5
The if statement is very simple.

when the user enters5When, the calculation result of the test expression number<0 is false, and the if does not execute the statements inside the body

C Language if ... else statement

The if statement can have an optional else block. The syntax of if..else statement is:

if (test expression) {
    //The statements to be executed when the test expression is true
}
else {
    // If the test expression is false, the following statements will be executed
}

How does the if ... else statement work?

If the calculation result of the test expression (test expression) is true,

  • The statements within if will be executed.

  • The statements in the else of the body will be skipped, that is, the statements in else will not be executed.

If the value of the test expression (test expression) is false,

  • The statements within else will be executed

  • The statements in the if body will be skipped, that is, they will not be executed.

Example2:if ... else statement

// Check if an integer is odd or even
#include <stdio.h>
int main() {
    int number;
    printf("Please enter an integer: ");
    scanf("%d", &number);
    //If the remainder is 0, it is true
    if (number%2 == 0) {
        printf("%d is an even number.", number);
    }
    else {
        printf("%d is an odd number.", number);
    }
    return 0;
}

Output result

Enter an integer: 23
23 it is an odd number.

when the user enters23when, (test expression) test expression number%2The evaluation result of ==0 is false. Therefore, the statements in the else body will be executed.

C language if...else if...else statements

if...else statements execute two different pieces of code depending on whether the test expression (test expression) is true or false. It is suitable for situations where you must choose from more than two possibilities.

if...else if...else allows you to check multiple test expressions and execute different statements. It is suitable for situations where you need to choose from more than two possibilities.

Syntax of if...else if...else

if (test expression1) {
   // Statement
}
else if (test expression2) {
   // Statement
}
else if (test expression3) {
   // Statement
}
.
.
else {
   // Statement
}

Example3: C language if...else if...else example

//The program uses =, > or < symbols to compare two integers
#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);
    //Check if two integers are equal
    if (number1 == number2) {
        printf("Result: %d = %d", number1, number2);
    }
    //Check number1Is greater than number2.
    else if (number1 > number2) {
        printf("Result: %d > %d", number1, number2);
    }
    //If both of the above test expressions are false, execute the following statements
    else {
        printf("Result: %d < %d", number1, number2);
    }
    return 0;
}

Output result

Enter two integers: 12
23
Result: 12 < 23

Nested if...else statements in C language

Another if...else statement can be included in the body of another if...else statement.

Example4Nested if...else demonstration

The program below provides an example of using any if...else ladder involving two integers <, >, and =, but we will use nested if...else statements to solve this problem.

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);
    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d", number1, number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d", number1, number2);
    }
    return 0;
}

If the body of an if...else statement contains only one statement, brackets {} can be omitted.

For example, the following code

if (a > b) {
    print("Hello");
}
print("Hi");

Is Equivalent to

if (a > b)
    print("Hello");
print("Hi");