Open In App

C if else Statement

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The if else in C is an extension of the if statement which not only allows the program to execute one block of code if a condition is true, but also a different block if the condition is false. This enables making decisions with two possible outcomes.

Let’s take a look at an example:

C
#include <stdio.h>

int main() {
    int n = 10;

    if (n > 5) {
        printf("%d is greater than 5",n);
    }
  	else {
        printf("%d is less than 5",n);
    }

    return 0;
}

Output
10 is greater than 5

Explanation: In the above program, the condition checks if the variable n is greater than 5. If true, if block is executed and it prints that n is greater than 5. If it evaluates to false, the else block is executed, printing that n is less than 5. Since n is 10, it prints “10 is greater than 5”.

Syntax of if-else

if (condition) {
// Code to execute if condition is true
}
else {
// Code to execute if condition is false
}

If the condition is true, then the code inside the if block is executed, otherwise the code inside the else block is executed. Any non-zero and non-null values are assumed to be true, and zero or null values are assumed to be false.

Flowchart of the if-else statement

The below flowchart explains the if else works in C:

flowchart of if-else statement in C

Flowchart of if-else in C

Examples of if else

The below examples illustrate how to use the if else statement in C programs

Check if a Number is Negative

C
#include <stdio.h>

int main() {
    int n = -7;

  	// If the number is negative
    if (n < 0)
        printf("Negative");
  
  	// If the number is not negative
  	else
        printf("Not Negative");

    return 0;
}

Output
Negative

Explanation: In this program, the if statements check if the given number n is less than 0 which means that the number is negative. As n is negative, it prints the if block. But if the number n was positive, the else block would have been executed.

Also, we can see than we have skipped { } braces. As long at the block only contains the single statement, we can skip the curly braces.

Check if Integer Lies in the Range

C
#include <stdio.h>

int main() {
    int n = 6;

    // Check if the number lies in the range [10, 20]
    if (n >= 10 && n <= 20) {
        printf("%d lies in range.", n);
    }
  	else {
        printf("%d does not lie in range.", n);
    }

    return 0;
}

Output
6 does not lie in range.

Find Largest Among Three Numbers

C
#include <stdio.h>

int main() {
    int a = 1, b = 2, c = 11;

    // Finding the largest by comparing using
    // relational operators with if-else
    if (a >= b) {
        if (a >= c)
            printf("%d", a);
        else
            printf("%d", c);
    }
    else {
        if (b >= c)
            printf("%d", b);
        else
            printf("%d", c);
    }

    return 0;
}

Output
11


Next Article
Article Tags :

Similar Reads