Thursday, July 05, 2012

The else if statement

Previously we have learnt about nested if else statement, the else if statement works some what like the nested if else statement. But it avoids the complication of the nested if else statement. In case of nested conditional operator, the programmer may indulge in the complication of the program. But a good programmer should always look forward to write a program a very easy and short way as possible. Thus else if statement is one such way to make a program simpler and easier for a developer to develop a program.
Let us understand, the utility of else if statement with an example.

A student has scored marks for five individual subjects, which needs to be entered by the user. The system will calculate the average marks of scores of five subjects and will output in the following manner:

If marks >= 80, the output will be GRADE A

If marks<80 and marks>=70, the output will be GRADE B

If marks<70 and marks>=60, the output will be GRADE C

If marks<60 and marks>=50, the output will be GRADE D

If marks<50 and marks>=40, the output will be GRADE E

If marks<40, the output will be FAIL

We will first solve the above program using nested if else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3, m4, m5, avg;
clrscr();
printf(“Enter the marks of five subjects: “);
scanf(“%d %d %d %d %d”, &m1, &m2, &m3, &m4, &m5);
avg=(m1+m2+m3+m4+m5)/5;
printf(“Average = %d\n”, avg);
/* Program using Nested if else statement */
if(avg>=80)
   printf(“GRADE A”);
else
   {
      if(avg>=70)
         printf(“GRADE B”);
      else
         {
            if(avg>=60)
               printf(“GRADE C”);
            else
               {
                  if(avg>=50)
                     printf(“GRADE D”);
                  else
                     {
                        if(avg>=40)
                           printf(“GRADE E”);
                        else
                              printf(“FAIL”);
                     }
               }
         }
   }
getch();
}
Now, we will solve the program using else if statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3, m4, m5, avg;
clrscr();
printf(“Enter the marks of five subjects: “);
scanf(“%d %d %d %d %d”, &m1, &m2, &m3, &m4, &m5);
avg=(m1+m2+m3+m4+m5)/5;
printf(“Average = %d\n”, avg);
/* Program using else if clause */
if(avg>=80)
   printf(“GRADE A”);
else if(avg>=70)
   printf(“GRADE B”);
else if(avg>=60)
   printf(“GRADE C”);
else if(avg>=50)
   printf(“GRADE D”);
else if(avg>=40)
   printf(“GRADE E”);
else
   printf(“FAIL”);
getch();
}
Here we see that, both the programs serves the same purpose, but the second program is much simpler than the first one having nested if else statement. These has been portrayed with a small example, thus in case of bigger programs, the complicacy would have been much more. Thus it should be the duty of a good programmer to find the shortest and easiest way to write any program.

4 comments:

  1. I tried NESTED IF ELSE STATEMENT! BUt it gives an error DECLARATION TERMINATED INCORRECTLY!
    How can it solve?

    ReplyDelete
    Replies
    1. Yes, the error may be due to the double inverted commas, which we are using in the web version are different, please change the double inverted commas, to get the corrected output. If the problem still persists, feel free to get in touch...

      Thanks for reading my blog.
      You may get tech related information and updated from www.techloger.com

      Delete
  2. boht alla yr great info

    ReplyDelete

Please give your valuable comments or queries if you fell its helpful or if you like the contents of the site. Thanks...