C if else Statement (original) (raw)

Last Updated : 18 Oct, 2025

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.

#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".

**Working of 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

**Negative Number check using If-else statement

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;

}

`

**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.

**Note: If 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;

}

`

Largest Among Three Numbers Using If-else

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;

}

`

Try It Yourselfredirect icon