if...else statement in C programming with example programs

This article was created and published to describe the following three conditional statements available in the C programming language:

The if statement in C

The "if" statement in the C programming language is used when we need to execute some block of code only if the specified condition evaluates to true. The general form of the "if" statement is:

if(conditional_expression)
{
   // body of the "if"
}

For example:

#include <stdio.h>
int main()
{
   int x = 10;
   if(x%2 == 0)
   {
      printf("%d is an even number", x);
   }
   return 0;
}

In the above program, since the condition "10%2 == 0" (on putting the value of "x") evaluates to true, the program flow enters into the if's body and executes all the codes available in its body. In the above program, since there is only one statement available in its body, that statement will be executed, which prints the text "10 is an even number." Therefore, the output should be:

10 is an even number

The if...else statement in C

Unlike the "if" statement, in the case of an "if...else" statement, there is another block called the "else" block, which is used to execute some block of code if the specified condition in the "if" statement evaluates to false. Therefore, you can say that this is the extended version of the "if" statement, which is used to execute some block of code if the specified condition evaluates to true and otherwise execute other blocks of code if the specified condition evaluates to false.

The general form of the "if...else" statement is as follows:

if(conditional_expression)
{
   // body of the "if"
   
   // Program flow enters into this body
   // if the "conditional_expression"
   // evaluates to true.
}
else
{
   // body of the "else"
   
   // Otherwise, program flow enters into this body
   // if the "conditional_expression"
   // evaluates to false.
}

For example:

#include <stdio.h>
int main()
{
   int x = 11;
   if(x%2 == 0)
   {
      printf("%d is an even number", x);
   }
   else
   {
      printf("%d is an odd number", x);
   }
   return 0;
}

The output should be:

11 is an odd number

I just changed the value of "x" to 11 to demonstrate the "if...else" concept in C.

The if...else if...else statement in C

When we need to evaluate multiple conditions and execute a block of code associated with the statement whose condition evaluates to true, we use the "if...else if...else" statement. If none of the conditions are met, the statement in the "else" block will be executed.

The general form of the "if...else if...else" statement is as follows:

if(conditional_expression1)
{
   // body of the "if"
   
   // Program flow enters into this body
   // if the "conditional_expression1" 
   // evaluates to true.
}
else if(conditional_expression2)
{
   // body of the "else if"
   
   // Otherwise, program flow enters into this body
   // if the "conditional_expression2"
   // evaluates to true.
}
else if(conditional_expression3)
{
   // body of the "else if"
   
   // Otherwise, program flow enters into this body
   // if the "conditional_expression3"
   // evaluates to true.
}
.
.
.
else if(conditional_expressionN)
{
   // body of the "else if"
   
   // Otherwise, program flow enters into this body
   // if the "conditional_expressionN"
   // evaluates to true.
}
else
{
   // body of the "else"
   
   // If none of the conditions are true,
   // program flow enters into this body.
}

First, the condition of the "if" will be evaluated; if it evaluates to false, then the condition of the first "else...if" will be evaluated; if it also evaluates to false, then the condition of the second "else...if" will be executed, and so on. If any condition evaluates to true, then the program flow enters the body associated with that condition's statement, and the rest of all the conditions, including the "else" block, will be skipped. Let me give an example:

#include <stdio.h>
int main()
{
   int x = 11;

   if(x<0)
   {
      printf("%d is a negative number", x);
   }
   else if(x == 0)
   {
      printf("%d is zero", x);
   }
   else if(x < 10)
   {
      printf("%d is a positive number.", x);
      printf("\nThe value is less than 10");
   }
   else if(x > 10 && x < 100)
   {
      printf("%d is a positive number.", x);
      printf("\nThe value is greater than 10 but less than 100");
   }
   else
   {
      printf("%d is a positive number.", x);
      printf("\nThe value is greater than 100.");
   }

   return 0;
}

The output should be:

11 is a positive number.
The value is greater than 10 but less than 100.

C++ if...else Example Programs

C Online Test


« Previous Tutorial Next Tutorial »


Liked this article? Share it!





Like/follow us on social media for updates!




Like/follow us on social media for updates!