Friday, February 13, 2015

Nested if else in C

In the previous tutorial we have learnt about if-else statements. Those statements provide the flexibility to check for two possible faces of answer. But it is also possible that there will be more than two options for the answer. To make things more precise, take an example of school grading system. Suppose if you want to create a program that will print the grade of student based on his/her percentage marks. Then in that case you will need at least 4 conditions to check.

So to put those things on work we have to use nested if-else statements.


What are nested if-else statements?

As the name suggests, nesting means writing if-else statements inside another if or else block. Basically there is no limit of nesting. But generally programmers nest up to 3 blocks only.

General form of nested if-else statements is given below.

if (condition)
{
Statement 1
Statement 2 and so on
}

else
{
if (condition)
{
Statement a
Statement b
}
else
{
Statement c
Statement d
}
}

It’s a bit complex structure for beginners so lets try to understand its general form.

As you can see, I have nested another if-else block inside one else block. So while executing in this form. The compiler first check the condition if (primary or first) block. If it fails then it will move on to the else block. In the else block it will check the condition of secondary if statement, if it also fails then it will execute the statements under else block.

In our general form I have nested if-else block in else block. You can also nest that if-else statement under first if statement. This will also work.

Now lets try implement our knowledge in one program.

Question: Take one number from the user. Check it whether it is negative, zero or positive and print the message for it.


#include <stdio.h>

void main()
{
int num;
printf("Enter any number:");
scanf("%d",&num);

if (num < 0)
printf("number is negative");

else
{
if (num==0)
printf("number is 0");
else
printf("number is positive");
}
}


Output

Nested if-else in C - Output

Explanation

I have deliberately write that program which is similar to the last one. As I want to show the importance of if-else nesting inside C programming.

Initial statements of the program are self-explainable. So I will start my explanation with if-else statement.
  • In the first if statement, I have given a condition i.e. num<0, it will check the number whether it is negative or not. If it is negative then it will print the message "number is negative". But if condition fails then it will skip the statements under if block.
  • Am I forget to write curly braces {} after if statement? The program will also work by dropping them in our case. This is because the default scope of if statement is one statement after it. As I have written only one statement, so there is no need to use curly braces in it. However if you write more than one statements under that block then you have to insert curly braces.
  • After that I gave another else block. And inside that else block I have nested one if-else block. It means after entering the control inside else block it will check the condition of 2nd if block. If the condition turns out to be true then it will print the message "number is 0". Otherwise the control will transfer to the else block and it will print "number is positive".
  • In the above program you can clearly see the secondary if-else block. Because I have indented the 2nd if-else block to increase the readability of program. So it is advised to use indentation while using nested if-else.