The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Decision Statements

Using decision statements, we can make our programs execute certain statements depending on some conditions. Next we illustrate the decision statements supported by C.

' if ' statement
It consist of a boolean expression and we execute the set of statements in the ' if ' block depending on the value (True/False) of the boolean expression. Following program demonstrates the use of ' if ' statement :

/*
 * This program takes marks obtained as input anf if the marks is less than
 * 35, then a grace of 5 marks is added.
 */

#include <stdio.h>

int main() {
   int marks;
   printf("Enter Marks : ");
   scanf("%d",&marks); /* take input from user */
   /* check the condition */
   if ( marks < 35 ) { /* if block starts here */
      /* condition is true ( marks is less than 35 ), so add the grace marks */
      marks += 5;
   } /* if block ends here */
   printf("\nFinal Marks : %d\n", marks);
   return 0;
}
Run this program in your system to take input at run-time  


' if ... else ' statement
This statement is used when there are multiple conditions depending on which different block of statements are to be executed. If the condition is true, then statements in the if block is executed, otherwise statements in the else block is executed. See the following program to understand the use of ' if ... else ' construct :

/*
 * This program finds the larger of two integers
 */

#include <stdio.h>

int main() {
   int x = 7, y = 9;  
   /* test a condition (x > y) to take decision */
   if ( x > y ) { /* if block starts */
      /* condition is true ( x is greater than y ), so print the following */
      printf("Greater no. is : %d\n", x);
   } /* if block ends */
   else { /* else block starts */
      printf("Greater no. is : %d\n", y);
   } /* else block ends */
   return 0;
}

When there are more than one if condition are to be evaluated, then we use a chain of if ... else statements. Following program illustrates the use case :

/* 
 * This program assigns grade to students depending on marks obtained
 * Marks Obtained     Grade
 *     >= 90            A
 * >= 80 and < 90       B
 * >= 70 and < 80       C
 * >= 60 and < 70       D
 * >= 50 and < 60       E
 *     < 50             F
 */

#include <stdio.h>

int main() {
   int marks = 73;
   char grade;
   /* Assign the grade depending on marks */
   if ( marks >= 90 ) {
      grade = 'A';
   }
   else if ( marks >=80 && marks < 90 ) {
      grade = 'B';
   }
   else if ( marks >=70 && marks < 80 ) {
      grade = 'C';
   }
   else if ( marks >=60 && marks < 70 ) {
      grade = 'D';
   }
   else if ( marks >=50 && marks < 60 ) {
      grade = 'E';
   }
   else if ( marks < 50 ) {
      grade = 'F';
   }
   printf("Grade Assigned is %c", grade);
   return 0;
}

' switch ' statement
switch statement is used when a condition or a variable has to be tested for equality against multiple values. Following program illustrates the use of switch construct :

/* 
 * This program gives comments to students depending on their grades
 * Grade   Comments
 *   A     Outstanding
 *   B     Excellent 
 *   C     Very Good
 *   D     Average
 *   E     Below Average
 *   F     Fail
 */

#include <stdio.h>
#include <ctype.h>

int main() {
   char grade;
   printf("Enter your GRADE : ");
   scanf("%c",&grade); /* input the grade */
   /* convert the entered character to uppercase if the grade is entered in 
      lowercase characters.
      toupper is defined in <ctype.h> header file */
   grade = toupper(grade);
   /* Print comment depending on grade */
   switch (grade) { /* switch block starts here */
      case 'A' : printf("Outstanding\n");
                break; /* break out of switch block */
      case 'B' : printf("Excellent\n");
                break;
      case 'C' : printf("Very Good\n");
                break;
      case 'D' : printf("Average\n");
                break;
      case 'E' : printf("Below Average\n");
                break;
      case 'F' : printf("Fail\n");
                break;
      default  : printf("Invalid Grade\n");
                break;
   }
   return 0;
}
Run this program in your system to take input at run-time  


Nesting of Decision statements
We can use a decision statement inside the block of another decision statement. This is known as nesting. See the following program to understand nesting :

/*
 * This program calculates the bonus for an employee depending
 * on the following conditions :-
 * 1) Employee should be regular
 * 2) Bonus = (No. of years served * 1 month salary) if years served <= 3
 * 3) Bonus = ( 3 months salary ) / 2 if years served > 3
 */

#include <stdio.h>
#include <ctype.h>

int main() {
   char emp_type; /* Employee Type */
   int years_served;
   float salary, bonus;
   printf("Enter Employee Type (R/r for regular) : ");
   scanf("%c", &emp_type);
   emp_type = toupper(emp_type);
   if ( emp_type == 'R' ) { /* regular employee */
      printf("Enter no. of years served : ");
      scanf("%d", &years_served);
      printf("Enter salary : ");
      scanf("%f", &salary);
      if ( years_served <= 3 ) {
         bonus = salary * years_served;
      }
      else {
         bonus = ( salary * 3 ) / 2;
      }
      printf("Bonus : %f\n", bonus);
   }
   else { /* Non-regular employee */
      printf("Not elegible for bonus\n");
   }
   return 0;
}
Run this program in your system to take input at run-time  

Back | Next