Thursday, February 24, 2011

else if in c

Decision making is done by generally in four ways :
  1. By if statement
  2. switch statement
  3. conditional operator statement
  4. goto statement

if statement is good for decision making as it is very simple and it work on 2-ways decision (either this or that )
syntax
if(Test expression)

If the condition tested in if statement is true then it will execute the body of if statement or it will skip the body of if statement

Example
if (test expression )
{
       body part;
}

There are many types of if statements , depend on on which type of decision to be taken or what conditions are to be checked.

  • Simple if
  • if....else statement
  • Nested if …. else statement
  • else if ladder.
  • Simple if statement is given in the above example.


if....else statement

Structure of if...else

if  (test expression )
     {
         statement for true condition;
     }
else
    {
        statement for false condition;
    }
statements of program ;
While checking the test expression of if statement, for condition is true then if part is executed and for false condition else part is executed.

Nested if …. else statement

Structure of Nested if …. else

if(test expression-1 )
{
       if(test expression-2 )
          {
               statement for true condition;
          }
       else
          {
              statement for false condition;
          }
}
else
   {
         statement last;
   }
statements of program ;

When there are more condition to be checked and program is some what complicated this nested if..else structure is used.

  • Function of it is complicated. It will execute first “test expression-1” of if function , for true condition it will execute “test expression-2” of second if function , again condition is true then it will execute “statement for true condition;” and return “statements of program
  • suppose condition is false for “test expression-1”, then it will return else which contain“statement last;” ,and return “statements of program
  • suppose condition is false for “test expression-2”,and true for “test expression-1”,then it will return else which contain“statement for false condition;”,and return “statements of program









else if ladder

Structure of else if ladder

if(test expression-1 )
      statement-1;
else if(test expression-2 )
      statement-2;
else if(test expression-3 )
      statement-3;
else if(test expression-4 )
      statement-4;
else
      final statement;

statements of program ;

  • In else if ladder the condition are checked from top op to bottom in a sequence ,
  • As if condition is checked first and the followed by else if from top to bottom, if one condition is true,that statement is executed and program again returns to “statements of program ;
  • If all the condition are false the else is executed and final statement is executed as defolt statement
EXAMPLE
#include<stdio.h> // header files
#include<conio.h> //header files
void main()
{
  int marks ;
  clrscr(); //to clear output of previous program
  printf("\n\nenter your marks");
  printf("\n\n i will know grade and I m program\n\n ");
  scanf("\n\n%d",&marks);

  if(marks>=70)
   {
      printf("\n\nyou got distinsion");
   }
  else if(marks>=60)
  {
     printf("\n\nyou got first class");
  }
  else if(marks>=50)
  {
     printf("\nyou got second class");
  }
  else if(marks>=35)
  {
    printf("\nyou have just passed");
  }
  else
   printf("\nyou have failed");
   printf("\n\n\n");
   getch(); // It will accept one character from keyboard to end the program
}

I Hope friends  this may help you to understand the program easily post you suggesion and comments

No comments:

Post a Comment