C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Nested if else and else if ladder in C


Abstraction:
          This tutorial describes about Nested if else and else if ladder in C programming with examples



Nested If

Like the name indicate, nested else if contains if statement inside an if statement. If we want to check a condition even a condition is hold, then we use nested if.

Let us see with an example:
   
#include <stdio.h>
#include <stdlib.h>
int main()
{
     int n1 = 30,n2 = 20 ,n3 = 10;       
     if(n1 > n2)
     {
       if(n1>n3)
         {
           printf("Largest is %d",n1);
         }
      }
}

Here n1(30) is greater than n2(20), so again check with the value of n3(10). Then decided a decision( printf("Largest is %d",n1); )

else if Ladder

In previous lesson, we learned the working of simple if statement. There, we saw a single if condition and an else block. But in case more than one condition, how can we code? Here we use if else ladder statement.

Let us see with an example:

Generate grade from mark:
More than or equal to 80: A grade
More than or equal to 60: B grade
More than or equal to 40: C grade
Less than 40 and greater than 0: D grade
For other mark : Invalid


#include <stdio.h>
#include <stdlib.h>
int main()
{
  int mark =0;
  printf("Enter the mark: ");
  scanf("%d",&mark);
  if(mark >= 80)
      printf("Grade is A ");
  else if(mark >= 60)
      printf("Grade is B");
  else if(mark >= 40)
      printf("Grade is C");
  else if(mark < 40 && mark >= 0)  
      printf("Grade is D");
  else
      printf("Invalid Mark");
}

Where 'else if' statement works only when its previous statement does not hold the condition.

That is,

else if(mark >= 60)
printf("Grade is B");

This condition works only its previous condition is false

if(mark >= 80)
printf("Grade is A ");

And final ‘else’ block works only when all the conditions are does not hold. Note than where no brazes were used for 'else if' block, because there only one statement in every block

Sample Input and output with screenshots:

else if ladder in C

Another Example:

Check whether the given number is positive, negative or zero.


#include <stdio.h>
#include <stdlib.h>
int main()
{
  int no =0;
  printf("Enter the mark: ");
  scanf("%d",&no);
  if(no > 0)
       printf("The given number %d is Positive ",no);  
  else if(no <0)
       printf("The given number %d is Negative ",no);
  else
       printf("Given number is zero");
}


This  block will work

else if(no <0)
printf("The given number %d is Negative ",no);

If and only If this block  is not true:

if(no > 0)
printf("The given number %d is Positive ",no);


1 comment:

  1. very help full site i have completed my first year,first sem refering notes from this site and i got distinction in program in .
    Must refer notes from this site

    Thankyou YOUROWNCODE.COM because of them i am pass
    Thank you sooo much!!!!!

    ReplyDelete