Exforsys.com
 
Home Tutorials C Language
 

C Programming - Decision Making - Branching

 

The ELSE If Ladder

Page 2 of 2


The ELSE If Ladder

When a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form.


if (condition1)
   statement – 1;
else if (condition2)
   statement2;
else if (condition3)
   statement3;
else if (condition)
   statement n;
else
   default statement;
statement-x;

This construct is known as if else construct or ladder. The conditions are evaluated from the top of the ladder to downwards. As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder. When all the condition becomes false, the final else containing the default statement will be executed.




/* Example program using If else ladder to grade the student according to the following rules.


Marks

Grade

70 to 100
60 to 69
50 to 59
40 to 49
0 to 39


DISTINCTION
IST CLASS
IIND CLASS
PASS CLASS
FAIL


Sample Code
  1. #include <stdio.h> //include the standard stdio.h header file
  2. void main () //start the function main
  3. {
  4.    int marks //variable declaration
  5.  
  6.    printf ("Enter marks\n") //message to the user
  7.    scanf ("%d", &marks) //read and store the input marks.
  8.  
  9.    if (marks <= 100 && marks >= 70) //check whether marks is less than 100 or greater than 70
  10.          printf ("\n Distinction")  //print Distinction if condition is True
  11.    else if (marks >= 60) //else if the previous condition fails Check
  12.          printf("\n First class") //whether marks is > 60 if true print Statement
  13.    else if (marks >= 50) //else if marks is greater than 50 print
  14.          printf ("\n second class") //Second class
  15.    else if (marks >= 35) //else if marks is greater than 35 print
  16.          printf ("\n pass class") //pass class
  17.    else
  18.          printf ("Fail") //If all condition fail apply default condition print Fail
  19. }
Copyright exforsys.com



The above program checks a series of conditions. The program begins from the first if statement and then checks the series of conditions it stops the execution of remaining if statements whenever a condition becomes true.


In the first If condition statement it checks whether the input value is lesser than 100 and greater than 70. If both conditions are true it prints distinction. Instead if the condition fails then the program control is transferred to the next if statement through the else statement and now it checks whether the next condition given is whether the marks value is greater than 60 If the condition is true it prints first class and comes out of the If else chain to the end of the program on the other hand if this condition also fails the control is transferred to next if statements program execution continues till the end of the loop and executes the default else statement fails and stops the program.


The Switch Statement:

Unlike the If statement which allows a selection of two alternatives the switch statement allows a program to select one statement for execution out of a set of alternatives. During the execution of the switch statement only one of the possible statements will be executed the remaining statements will be skipped. The usage of multiple If else statement increases the complexity of the program since when the number of If else statements increase it affects the readability of the program and makes it difficult to follow the program. The switch statement removes these disadvantages by using a simple and straight forward approach.

The general format of the Switch Statement is :


Switch (expression)
{
Case case-label-1;
Case case-label-2;
Case case-label-n;
………………
Case default
}

When the switch statement is executed the control expression is evaluated first and the value is compared with the case label values in the given order. If the label matches with the value of the expression then the control is transferred directly to the group of statements which follow the label. If none of the statements matches then the statement against the default is executed. The default statement is optional in switch statement in case if any default statement is not given and if none of the condition matches then no action takes place in this case the control transfers to the next statement of the if else statement.


Sample Code
  1. #include <stdio.h>
  2. void main ()
  3. {  
  4.    int num1, num2, result
  5.    char operator
  6.  
  7.    printf ("Enter two numbers")
  8.    scanf ("%d %d", &num1, &num2)
  9.    printf ("Enter an operator")
  10.    scanf ("%c", &operator)
  11.  
  12.    switch (operator)
  13.    {
  14.        case '+':
  15.           result = num1 + num2
  16.           break
  17.        case '-':
  18.           result = num1 - num2
  19.           break
  20.        case '*':
  21.           result = num1 * num2
  22.           break
  23.        case '/':
  24.           if (num2 != 0)
  25.              result = num1 / num2
  26.           else
  27.           {        
  28.              printf ("warning : division by zero \n")
  29.              result = 0
  30.           }
  31.           break
  32.        default:
  33.           printf ("\n unknown operator")
  34.           result = 0
  35.           break
  36.    }
  37.    printf ("%d", result)
  38. }
Copyright exforsys.com



In the above program the break statement is need after the case statement to break out of the loop and prevent the program from executing other cases.


The GOTO statement:

The goto statement is simple statement used to transfer the program control unconditionally from one statement to another statement. Although it might not be essential to use the goto statement in a highly structured language like C, there may be occasions when the use of goto is desirable.

Syntax


a>
goto label;
…………
…………
…………
Label;
Statement;
b> 
label;
…………
…………
…………
goto label;
 

The goto requires a label in order to identify the place where the branch is to be made. A label is a valid variable name followed by a colon.


The label is placed immediately before the statement where the control is to be transformed. A program may contain several goto statements that transferred to the same place when a program. The label must be unique. Control can be transferred out of or within a compound statement, and control can be transferred to the beginning of a compound statement. However the control cannot be transferred into a compound statement. The goto statement is discouraged in C, because it alters the sequential flow of logic that is the characteristic of C language.


Sample Code
  1. #include <stdio.h> //include stdio.h header file to your program
  2. main () //start of main
  3. {
  4.      int n, sum = 0, i = 0 // variable declaration
  5.      printf ("Enter a number") // message to the user
  6.      scanf ("%d", &n) //Read and store the number
  7.      loop: i++ //Label of goto statement
  8.      sum += i //the sum value in stored and I is added to sum
  9.      if (i < n) goto loop //If value of I is less than n pass control to loop
  10.      printf ("\n sum of %d natural numbers = %d", n, sum)
  11.      //print the sum of the numbers & value of n
  12. }
Copyright exforsys.com





First Page: C Programming - Decision Making - Branching


Read Next: C Programming - Decision Making - Looping



 

 

Comments


payal jain said:

  a real good explanation about all the topics covered(exellenet)
November 2, 2008, 8:49 am

1 said:

  all the topics are better explained in this book
December 17, 2008, 6:48 pm

chandan kumar jha said:

  this is the best
December 17, 2008, 6:58 pm

jacy said:

  its a very clear explanation. thank you! its great! keep up.
January 5, 2009, 8:54 pm

Mr.Patil Swapnil Raygonda said:

  Thank's for detail
January 16, 2009, 6:57 am

cindrella said:

  very gud xplanation....
January 18, 2009, 8:44 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape