LERAN C PROGRAMMING

Learn one of the most powerful programming languages in the world and become a rockstar developer.

IF ELSE Statement

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

The syntax of the if...else statement in C programming is:

if ( boolean_expression ) {
   /* statement(s) will execute if the boolean expression is true */
}  else  {
   /* statement(s) will execute if the boolean expression is false */
}
Explanation :
If the Boolean expression evaluates to true, then if block will be exexute otherwise else block will be executed.
// Program to display a number if it is negative
#include <stdio.h>
int main()
{
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // true if number is less than 0
    if (number < 0)
    {
        printf("You entered less than 0 number  %d.\n", number);
    }
	else
	{
		printf("You entered  greater than 0 number %d.\n", number);
	}
    printf("The if statement is easy.");
    return 0;
}
Output 1 :

Enter an integer: -2
You entered less than 0 number -2.
The if statement is easy.

If we enter the value greater than 0 then else block will be executed.

Output 2 :

Enter an integer: 4
You entered greater than 0 number 4.
The if statement is easy.

Syntax of nested if...else statement.

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

When using if...else if..else statements, there are few points to keep in mind −

  • An if can have zero or one else's and it must come after any else if's.

  • An if can have zero to many else if's and they must come before the else.

  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

The syntax of nested if...else is

if (test expression1) 
{
   // statement(s)
}
else if(test expression2) 
{
   // statement(s)
}
else if (test  expression3) 
{
   // statement(s)
}
.
.
else 
{
   // statement(s)
}
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main()
{
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);
    //checks if the two integers are equal.
    if(number1 == number2)
    {
        printf("Result: %d = %d",number1,number2);
    }
    //checks if number1 is greater than number2.
    else if (number1 > number2)
    {
        printf("Result: %d > %d", number1, number2);
    }
    //checks if both test expressions are false
    else
    {
        printf("Result: %d < %d",number1, number2);
    }
    return 0;
}
Output :

Enter two integers: 12
23
Result: 12 < 23

Nested if...else

It is possible to include an if...else statement inside the body of another if...elsestatement.

#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;
}


Previous Next
x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine