C Programs Tutorials | IT Developer
IT Developer

C Programming - if else statement in C



Share with a Friend

C Programming - if else statement in C

if-else Statement in C

The if-else statement in C allows you to execute one block of code if a condition is true and another block if the condition is false. It is used when you need to choose between two possible outcomes based on a condition.

Syntax of if-else Statement:

C

if (condition) {

    // Code to be executed if the condition is true

} else {

    // Code to be executed if the condition is false

}

  • condition: An expression that evaluates to true (non-zero) or false (zero).
  • Code block: The statements inside {} that execute based on whether the condition is true or false.

Example 1: Basic if-else Statement

C

#include <stdio.h>

int main() {

    int a = 10;

    // Check if a is greater than 5

    if (a > 5) {

        printf("a is greater than 5\n");

    } else {

        printf("a is not greater than 5\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 is true because a is 10.
  • Since the condition is true, the first block of code is executed, printing "a is greater than 5".

Output:

a is greater than 5

Example 2: if-else with False Condition

C

#include <stdio.h>

int main() {

    int a = 3;

    // Check if a is greater than 5

    if (a > 5) {

        printf("a is greater than 5\n");

    } else {

        printf("a is not greater than 5\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 is false because a is 3.
  • Since the condition is false, the else block is executed, printing "a is not greater than 5".

Output:

a is not greater than 5

Example 3: Using if-else with Logical Operators

You can combine multiple conditions in an if-else statement using logical operators like && (AND) and || (OR).

C

#include <stdio.h>

int main() {

    int a = 10, b = 5;

    // Check if a is greater than 5 and b is less than 10

    if (a > 5 && b < 10) {

        printf("a is greater than 5 and b is less than 10\n");

    } else {

        printf("Either a is not greater than 5, or b is not less than 10\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 && b < 10 is true because both conditions are true (a is 10 and b is 5).
  • The first block of code is executed, printing "a is greater than 5 and b is less than 10".

Output:

a is greater than 5 and b is less than 10

Example 4: if-else for Multiple Conditions (Nested if-else)

In some cases, you may need to check multiple conditions. You can nest if-else statements to handle this.

C

#include <stdio.h>

int main() {

    int a = 15;

    if (a > 20) {

        printf("a is greater than 20\n");

    } else {

        if (a > 10) {

            printf("a is greater than 10 but less than or equal to 20\n");

        } else {

            printf("a is less than or equal to 10\n");

        }

    }

    return 0;

}

Explanation:

  • The first condition a > 20 is false, so the program checks the second condition a > 10.
  • Since a is 15, the second condition is true, and "a is greater than 10 but less than or equal to 20" is printed.

Output:

a is greater than 10 but less than or equal to 20

Example 5: if-else Without {} (Single Statement Block)

If there is only one statement in the if or else block, you can omit the curly braces {}. However, using {} is recommended for clarity and to avoid errors.

C

#include <stdio.h>

int main() {

    int a = 3;

    // Check if a is greater than 5 (without using braces)

    if (a > 5)

        printf("a is greater than 5\n");

    else

        printf("a is not greater than 5\n");

    return 0;

}

Explanation:

  • Since there is only one statement in both the if and else blocks, the curly braces are optional.
  • The condition a > 5 is false, so the else block is executed, printing "a is not greater than 5".

Output:

a is not greater than 5

Important Points:

  • Condition: The condition inside the if statement should evaluate to either true (non-zero) or false (zero).
  • Single Statement Blocks: If there is only one statement in the if or else block, you can omit the curly braces {}, but it's better to use them for readability and to avoid errors.
  • Nesting: You can nest if-else statements to handle more complex decision-making.

Summary of if-else Statement:

  • The if-else statement is used when you need to execute one block of code if a condition is true and another block if it is false.
  • It is often used for binary decision-making and can be combined with logical operators to handle more complex conditions.