If Else Statement in C Programming

If Else Statement in C Programming

In this tutorial, you are going to learn about If Else Statement in C Programming.

In the preview of conditional statements if else statement is one of the most useful structure, so let’s learn about if else statement in brief.

Definition

If else statements are used when we wished to execute a certain statement or group of statement when a condition is true otherwise we move to next block of statements that is the else part.

Blocks in an If Else Structure

  1. If block which get executed when the condition inside the if is found out to be true.
  2. Else block which get executed when the statement in the condition is false.

Syntax of the if else statement:

if (condition)
{
     //Block of C statements here
     //These statements will only execute if the condition is true
}
else
{
//block of C statements here
//These statements will be executed if the condition is false.

Example of if else condition:

#include <stdio.h>
int main ()
{
    /* local variable definition */
   int a = 100;
   /* check the boolean condition */
   if( a < 50 )
{
      /* if condition is true then print the following */
      printf("a is less than 50n" );
   } else
{
      /* if condition is false then print the following */
      printf("a is not less than 50n" );
   }
  printf("value of a is : %dn", a);
  return 0;
}

If...Else 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.

Syntax of an If...Else If...Else Statement

if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else
{
   /* executes when the none of the above condition is true */
}

Example of an If...Else If...Else Statement

#include <stdio.h>
int main ()
{
   /* local variable definition */
   int a = 90;
   /* check the boolean condition */
   if( a == 30 )
{
      /* if condition is true then print the following */
      printf("Value of a is 30n" );
 }
else if( a == 50 )
{
      /* if else if condition is true */
      printf("Value of a is 50n" );
 }
else if( a == 70 )
{
      /* if else if condition is true  */
      printf("Value of a is 70n" );
 }
else
{
      /* if none of the conditions is true */
      printf("None of the values is matchingn" );
 }
   printf("Exact value of a is: %dn", a );
   return 0;
}

When the above code is compiled and executed, it produces the following result − None of the values is matching and Exact value of a is: 90


This post is written by - Vishal Vaibhab (BTech Chemical, IIT BHU). If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Previous Post Next Post

Contact Form