Easy Tutorial
❮ C Exercise Example13 C Function Puts ❯

Nested If Statements

C Decision

In C language, nested if-else statements are legal, which means you can use another if or else if statement within an if or else if statement.

Syntax

The syntax for nested if statements in C language:

if( boolean_expression 1)
{
   /* Executed when boolean expression 1 is true */
   if(boolean_expression 2)
   {
      /* Executed when boolean expression 2 is true */
   }
}

You can nest else if...else in a similar way to nesting if statements.

Example

#include <stdio.h>

int main ()
{
   /* Local variable definition */
   int a = 100;
   int b = 200;

   /* Check the boolean condition */
   if( a == 100 )
   {
       /* Check the condition if true */
       if( b == 200 )
       {
          /* Print the following statement if the condition is true */
          printf("The value of a is 100, and the value of b is 200\n" );
       }
   }
   printf("The exact value of a is %d\n", a );
   printf("The exact value of b is %d\n", b );

   return 0;
}

When the above code is compiled and executed, it produces the following result:

The value of a is 100, and the value of b is 200
The exact value of a is 100
The exact value of b is 200

C Decision

❮ C Exercise Example13 C Function Puts ❯