4/11/21

Nested IF-Else in C Programming


Hey Friend, in our last tutorial we have seen about if statements, and if-else statements. In the if statement only one condition is check and if the condition is true then the output will be shown on the display. And on the other hand, we have also seen that if you have two conditions then if-else were used to check those conditions. 

Now, during the high-level programming, you have to check more than two conditions. For Example, you have to write a program to check whether

  • Num1 is less than num2
  • Num1 is greater than num2
  • Num1 is equal to num2

In that case, you have to use a nested if-else statement on your programs. 

Nested IF – ELSE

A nested if-else statement is used when a series of decisions were used to check through your program. As the name suggests, Nesting means using an if-else construct within another one. But guys basically there is no limitation to nesting. But the programmers nest up to 3 blocks only. You could use more if you want. 

Syntax of the nested if-else statement

if (condition)

   {

      Statement 1

      Statement 2 and so on

   }

else

   {

      if (condition)

   {

      Statement a

      Statement b

   }

else

   {

Statement c

Statement d

   }

}

I know it is a bit complex for beginners but I will explain it to you in the easiest way. So let’s try to understand this. 

Look at the syntax. you see that I am nested the if-else statement into the else statement of the syntax. Now when the code is executed then the compiler first checks the if condition (primary if block) if the condition is false of if statement then the compiler goes on the else part. Now in the else block it will check the if condition (secondary if block). If the condition is not true of secondary if block, then the compiler is print the else (secondary else part) as output.

You can also nested if-else on the if(primary statement). If you haven’t understood it yet, then don’t worry let try it with a simple program.


Program to find out the greater number between two numbers. 

#include<stdio.h>

int main()

{

int a, b;

printf("Enter Number A & B = ");

scanf("%d%d", &a,&b);

if(a>b)

{

    printf("A is Greater than B");

}

else{

    if(b>a)

    {

        printf("B is Greater than A");

    }

    else{

        printf("Numbers are Equal");

    }

}

getch();

}

Output :- 

Explanation :-

First, we declare two integer variable name a and b. After the declaration of the variable name. We take input from the user in variable a and variable b. 

Now, we use nested if-else to check the greater number between a and b. So there are three possibilities that occur among checking the numbers. It should be:-

  • A is greater than B
  • B is greater than A
  • Both Number are Equal 

So, in the first if block of the part,  we check the condition. The condition is whether integer a is Greater than integer B if true then print the Message to the user that “A is greater than B”. if the first condition is true then the program is print the output.  Or if the condition is not true then the program will go on else block. 

Now, here are the two different conditions the first one is to check whether the integer B is Greater than A. If true then print the message to the user that “B is greater than B”. if this condition is not true then the else part is automatically executed by default.

Hope you all understood this. If you have any query then comment it down. For more keep visit here. 

1 comment:

  1. UI design collaborated, offering informed recommendations to improve the platform and ensure a productive engagement.
    design agency San Francisco

    ReplyDelete

Like us on facebook

blogger templatesblogger widgets

Follow on Twitter

Linkedin

Categories

Contact Form

Name

Email *

Message *

Mad About Computer. Powered by Blogger.