Skip to main content

if-else Statements in C

An if statement in C allows you to specify a block of code that will be executed only if a certain condition is true.

An else statement can be used in conjunction with an if statement to specify a block of code that will be executed if the condition in the if statement is false.

if statement and syntax

The if statement has the following syntax:

if (condition)
{
// code to be executed if condition is true
}

The condition in an if statement is a boolean expression that evaluates to either true or false. If the condition is true, the code in the if block will be executed. If the condition is false, the code inside if will be ignored.

For example:

The following code uses an if statement to check if the variable x is greater than 10. If it is, it prints "x is greater than 10".

int x = 15;

if (x > 10)
{
printf("x is greater than 10");
}

if else statement and syntax

You can also add an else clause to an if statement to execute a different block of code if the condition is false.

The syntax for this is:

if (condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}

For example:

The following code checks if the variable x is positive or negative and prints a message accordingly:

int x = 15;

if (x > 0)
{
printf("x is positive");
}
else
{
printf("x is negative");
}

Different types of conditional statements using if else

You can use the if statement with an else clause to create several different types of conditional statements. Here are a few examples:

Simple if-else statement

This is the most basic form of the if statement, where you test a single condition and execute one block of code if it's true, and a different block of code if it's false.

if (condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}

Example

#include <stdio.h>

int main(void)
{
int x = 10;

// Test the value of x
if (x > 0)
{
printf("x is positive\n");
}
else
{
printf("x is non-positive\n");
}

return 0;
}
Output:

Explanation

In this example, the if statement tests whether the variable x is greater than 0, and prints a message depending on the result. The output of this program will be "x is positive".

Nested if-else statement

You can use an if statement inside an else clause to create a nested if-else statement. This allows you to test for multiple conditions and execute different blocks of code based on the results.

if (condition1)
{
// code to be executed if condition1 is true
}
else
{
if (condition2)
{
// code to be executed if condition2 is true
}
else
{
// code to be executed if both condition1 and condition2 are false
}
}

Example

#include <stdio.h>

int main(void)
{
int x = 10;

// Test the value of x
if (x > 0)
{
// x is positive
if (x % 2 == 0)
{
printf("x is a positive even number\n");
}
else
{
printf("x is a positive odd number\n");
}
}
else
{
// x is not positive
if (x % 2 == 0)
{
printf("x is a non-positive even number\n");
}
else
{
printf("x is a non-positive odd number\n");
}
}

return 0;
}
Output:

Explanation

In this example, the outer if statement tests whether x is positive, and the inner if statement tests whether x is even or odd. The output of this program will be "x is a positive even number".

tip

You can nest if statements as deeply as you need to test for multiple conditions. However, it's generally a good idea to keep your code as simple and flat as possible to make it easier to read and understand.

If-else if-else statement

You can use an else if clause to chain multiple if statements together. This allows you to test for multiple conditions and execute different blocks of code depending on which condition is true.

if (condition1)
{
// code to be executed if condition1 is true
}
else if (condition2)
{
// code to be executed if condition2 is true
}
else if (condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if none of the above conditions are true
}

Example

#include <stdio.h>

int main(void)
{
int x = 10;

// Test the value of x
if (x == 0)
{
printf("x is equal to 0\n");
}
else if (x > 0)
{
printf("x is greater than 0\n");
}
else
{
printf("x is less than 0\n");
}

return 0;
}
Output:

Explanation

In this example, the if-else if-else statement tests the value of the variable x and prints a message depending on whether it is equal to 0, greater than 0, or less than 0. The output of this program will be "x is greater than 0".

execution

You can add as many else if clauses as you need to test for multiple conditions. The code in the final else clause will be executed if none of the conditions are true.