Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Decision Making - Branching Page - 3

Page 3 of 3
Author: Exforsys Inc.     Published on: 4th Apr 2006    |   Last Updated on: 28th Aug 2011

C Programming - Decision Making - Branching

Nested if Statement

Using “if...else statement” within another “if...else statement” is called ‘nested if statement’. “Nested if statements” is mainly used to test multiple conditions. They can be structured using following syntax:

Sample Code
  1. If(conditionA)
  2.         {
  3.         Statements
  4.         }
  5. Else if (conditionB)
  6.         {
  7.         statementB
  8.         }
  9. Else
  10.         {
  11.         statement
  12.         }
Copyright exforsys.com


Ads

Example #1:

Consider the case that you are asked to program a university marking system which identifies student’s grade on response to user input. Here is how grades should be grouped: 40+ E, 50-59 D , 60-69 C , 70-79 B , 80+ A

Code:

Sample Code
  1. int Mark;
  2.         printf("Please Enter the grade between 0-100?n");
  3.         scanf("%d",&Mark);
  4.         if(Mark >= 40 && Mark <50)
  5.         {
  6.                 printf("your grade is E n");
  7.         }
  8.         else if(Mark>=50 && Mark<60)
  9.         {
  10.                 printf("Your grade is D n");
  11.         }
  12.         else if(Mark>=60 && Mark<70)
  13.         {
  14.                 printf("Your grade is C n");
  15.         }
  16.         else if(Mark>=70 && Mark<80)
  17.         {
  18.                 printf("Your grade is B n");
  19.         }
  20.         else if(Mark>=80 && Mark<90)
  21.         {
  22.                 printf("Your grade is A n");
  23.         }else
  24.                 printf("you have failed");
Copyright exforsys.com


Figure 4 code 4 screenshot

Description:

This code checks from lowest boundary and checks for every grade, once one of conditions matches the mark the remaining conditions will be ignored. If none of the conditions match, it will then print out the message “you have failed”.

The Switch Statement

“Switch statement” is another type of “conditional statement” used by programmers. Switch is widely used for menus.

Sample Code
  1. Switch (variable)
  2.         {
  3.         Case 1: statement1 break;
  4.         Case 2: statement 2 break;
  5.         Case 3:statement 3 break;
  6.         .
  7.         .      
  8.         .
  9.         Default: default statement break;
  10.         }
Copyright exforsys.com


Tips:

“break” statement is very important. If it is not used properly all the code after the matched condition will be executed incorrectly. For example: looking at syntax 4; if the break for case 2 was missing, all the codes after and including case 2 would be executed until the next break statement was reached.

Example #1:

This example will cover a very basic character based menu that responds to a , b, c and d; it also warns the user if any other characters are used.

Code:

Sample Code
  1. char response;
  2.         printf("Please Enter a,b,c or d?n");
  3.         scanf("%c",&response);
  4.        
  5.         1:switch(response)
  6.         {
  7.         2:case 'a': printf("you have inputed 'a'"); break;
  8.         3:case 'b': printf("you have inputed 'b'"); break;
  9.         4:case 'c': printf("you have inputed 'c'"); break;
  10.         5:case 'd': printf("you have inputed 'd'"); break;
  11.         6:default: printf("unrecognised character"); break;
  12.  
  13.         }
Copyright exforsys.com


Figure: 5 simple menu

Description:

“statement 1” indicates that “switch” will be applied on ‘response’. “Statement 2” defines the action needed in case ‘response’ is holding the value of ‘a’. “statement 3,4,5” define action in case ‘response’ is holding value of ‘b’, ’c’ or ‘d’ respectively. “Statement 6” is defining the action needed if none of the cases meet the conditions.

About this tutorial

All codes presented in this tutorial are snippets therefore they will not work unless they are put into acceptable structure. Acceptable structure includes stdio.h library( or any other relevant library) and main() function in the program. All the snippets in this tutorial are tested by Microsoft Visual C++ Express 2010.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments