Google News
logo
Decision Making Statements In C Language
These statements change the flow of execution depending on a given logical condition. Used to execute or skip some set of instructions based on given condition.
1) Simple “if” statement :

“if” statement is a powerful decision making statement, and is used to control the flow of execution of statements.

  Syntax :
if (Condition or test expression)
Statement;
Rest of the program 

(OR)

if (Condition or test expression) 
{
Statement;
}
Rest of the program; 

It is basically a “Two-way” decision statement (one for TRUE and other for FALSE)

It has only one option.

The statement as executed only when the condition is true.

In case the condition is false the compiler skips the lines within the “if Block”.

The condition is always enclosed within a pair of parenthesis i.e. ( ) ,

The conditional statement should not terminated with Semi-colons (ie ;)

The Statements following the “if”-statement are normally enclosed in Curly Braces {}.

The Curly Braces indicates the scope of “if” statement. The default scope is one statement. But it is good practice to use curly braces even with a single statement.

The statement block may be a single statement or a group of statements.

If the Test Expression / Conditions are TRUE, the Statement Block will be executed and executes rest of the program.

If the Test Expression / Condition are FALSE, the Statement Block will be skipped and rest of the program executes next.

Flow chart for “if” statement :
Decision Making System
  Program: Write a program to check equivalence of two numbers. Use “if” statement.
# include<stdio.h> 
 # include<conio.h>  
void main( ) 
 { 
     int  m,n; 
     clrscr( );  
     printf(“\n Enter two numbers:”); 
     scanf(“%d %d”, &m, &n); 
    	   if((m-n)==0) 
  	   printf(“\n two numbers are equal”);  
     getch();
}
Output :

Enter two numbers: 5 5
Two numbers are equal.

(2) “if-else” Statement :

It is observed that the “if” statement executes only when the condition following if is true. It does nothing when the condition is false.

In if-else either True-Block or False – Block will be executed and not both. The “else” Statement cannot be used without “if”.

  Syntax :
if ( Test Expression or Condition ) 
      { 
         Statements;  /*true block (or) if block */ 
       }
      else 
       { 
         Statements;   /* false block (or) else block */ 
       } 
Flow chart :
Decision Making System
  Program: Write a program to print the given number is even or odd.
# include<stdio.h>
# include<conio.h>
main( ) 
   { 
 	 int n;  
clrscr( ); 
printf(“Enter a number :”); 
scanf(“%d”, &n); 
 if( (n%2)==0 )  
          printf(“\n The given number is EVEN ”); 
     	 else 
          printf(“\n The given number is ODD ”); 
   	 getch( );
   } 
Output :

Run 1 :
Enter a number : 24
The given number is EVEN
Run 2: /* that means one more time we run the program */
Enter a number : 17
The given number is ODD

  Program: Write a program to print the given number is even or odd.
# include<stdio.h> 
# include<conio.h> 
void main( )  
   {  
  	int a,b;  
clrscr( );  
printf(“Enter Two numbers:”);  
scanf(“%d%d”, &a,&b);  
 if( a>b )  
       printf(“\n %d is largest number”,a);  
   else  
       printf(“\n %d is largest number”,b);  
     getch( ); 
   }  
Output :

Enter Two numbers: 13 30
30 is largest number

(3) Nested “if–else” Statement :

Using of one if-else statement in another if-else statement is called as nested if-else control statement.

When a series of decisions are involved, we may have to use more than one if-else statement in nested form.

  Syntax :
if ( Test Condition1) 
 {  
if ( Test Condition2) 
   { 
     Statement -1;
   }
else 
  {
     Statement -2; 
  } 
}
else 
{ 
 if ( Test Condition3) 
  { 
   Statement -3;
  }
else
{
Statement-4;
 }
} /* end of outer if-else *
  Syntax :
# include<stdio.h>
# include<conio.h>
 main( )  {  
      float  a,b,c; 
      printf(“Enter Three Values:”); 
      scanf(“%f%f%f”, &a, &b, &c); 
      printf(“\n Largest Value is:”) ;   
              
    if(a>b) { 
             	 if(a>c)  
                  printf(“ %f ”, a); 
               else 
                  printf(“ %f ”, c); 
             }
     else { 
          if (b>c) 
                printf(“ %f ”, b); 
          else 
              printf(“ %f ”, c); 
        }  

getch();
     } 

If Test Condition-1 is true then enter into outer if block, and it checks Test Condition2,if it is true then Statement-1 executed if it is false then else block executed i.eStatement-2.

If Test Condition -1 is false then it skips the outer if block and it goes to else block and Test Condition-3 checks if it is true then Statement-3 executed, else Statement-4 executed.

Flow chart :
Decision Making System
  Program: Program to select and print the largest of the three float numbers using nested “if else” statements.
if ( Test Condition1) 
 {  
if ( Test Condition2) 
   { 
     Statement -1;
   }
else 
  {
     Statement -2; 
  } 
}
else 
{ 
 if ( Test Condition3) 
  { 
   Statement -3;
  }
else
{
Statement-4;
 }
} /* end of outer if-else *
Output :

Run 1: Enter three values: 9.12 5.34 3.87
    Largest Value is: 9.12
Run 2: Enter three values: 45.781 78.34 145.86
    Largest Value is: 145.86

  Program: Program to select and print the largest of the three float numbers using nested “if else” statements.
# include<stdio.h>
# include<conio.h>
 main( )  {  
      float  a,b,c; 
      printf(“Enter Three Values:”); 
      scanf(“%f%f%f”, &a, &b, &c); 
      printf(“\n Largest Value is:”) ;   
              
    if(a>b) { 
             	 if(a>c)  
                  printf(“ %f ”, a); 
               else 
                  printf(“ %f ”, c); 
             }
     else { 
          if (b>c) 
                printf(“ %f ”, b); 
          else 
              printf(“ %f ”, c); 
        }  

getch();
     } 
Output :

Run 1: Enter three values: 9.12 5.34 3.87
    Largest Value is: 9.12
Run 2: Enter three values: 45.781 78.34 145.86
    Largest Value is: 145.86

(4) The “else – if” Ladder :

This is another way of putting if‘s together when multiple decisions are involved. A multipath decision is a chain of if‘s in which the statement associated with each else is an if. Hence it forms a ladder called else–if ladder.

if else-if, statement is used to execute one code from multiple conditions.

  Syntax :
if (Test Condition -1) 
 { 	
Statement -1;  
   }
    else if ( Test Condition -2) 
{ 
Statement -2;
}
 else if ( Test Condition -3) 
{
 Statement -3;  
}
   : 
   : 
   : 
   :    
  else  if ( Test Condition –n)  
{
Statement –n;
}
  
  else  
{
default statement; 
 }
Rest of the Program Statements-X; 

The above construction is known as else if ladders.

The conditions are evaluated from top to bottom.

As soon as a true condition is found, the statement associated with it is executed and

The control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder).

When all the “n” conditions become false, then the final else containing the default statement will be executed.

Flow chart :
Decision Making System
  Program: Write a program to read three numbers and find the largest one by using “else-if” ladder.
# include<stdio.h>  
# include<conio.h> 
void main( ) 
{  
  int  a, b, c  
clrscr ( ) ;  
printf(“Enter 1st  number:”);  
scanf(“%d”, &a);  
printf(“Enter 2nd  number:”);  
scanf(“%d”, &b);  
printf(“Enter 3rd  number:”);  
scanf(“%d”, &c);  
   if ((a>b) && (a>c))  
       	printf(“Highest Number is: %d”, a);  
    	 else if ((b>a) && (b>c))  
       	printf(“Highest Number is: %d”, b);  
   	  else  
        printf(“Highest Numbers is: %d”, c);  
getch( );  
}
Output :

Enter 1st number: 52
Enter 2nd number: 90
Enter 3rd number: 74
Highest Numbers is: 90

(5) The “switch-case” Statement :

Switch is another conditional control statement used to select one option from several options based on given expression value; this is an alternative to the if-else-if ladder.

The switch statement causes a particular group of statements to be chosen from several available groups.

The selection is based upon the current value of an expression which is included with in the switch statement.

The switch statement is a multi-way branch statement.

In a program if there is a possibility to make a choice from a number of options, this structured selected is useful.

The switch statement requires only one argument of int or char data type, which is checked with number of case options.

The switch statement evaluates expression and then looks for its value among the case constants.

If the value matches with case constant, then that particular case statement is executed.

If no one case constant not matched then default is executed.

Here switch, case and default are reserved words or keywords.

Every case statement terminates with colon “:”.

In switch each case block should end with break statement, i.e.

  Syntax :
Switch (variable or expression)  
   	 {  
     	   Case Constantvalue-1:Block -1;  
                 (Or)  
                 Statement-1;  
                 break;  

      	  Case Constantvalue-2:Block -2;  
    		(Or)  
            	Statement-2;  
                break;
	_ _ _ _ _ _ _ _ 
        _ _ _ _ _ _ _ _

       	 Case Constant value-n:	 Block -n;  
   		(Or)  
        	Statement-n;  
       		break; 

      	 default: default – block;
	}
Flow chart :
Decision Making System
  Program: Write a program to provide multiple functions such as 1.Addition, 2.Subtraction, 3.Multiplication, 4. Division, 5.Remainder, 6.Larger out of two, 7. Exit, using “switch” statement.
# include<stdio.h> 
# include<conio.h> 
main( ) 
{ 
     int a, b, c, ch; 
     clrscr ( ) ; 
     printf(“\t = = = = = = = = = = = = = =”); 
     printf (“n\t MENU”); 
     printf(“\n\t=  = = = = = = = = = =”); 
     printf(“\n \t [1] ADDITION” ); 
     printf(“\n \t [2] SUBTRACTION” ); 
     printf(“\n \t [3] MULTIPLICATION” ); 
     printf(“\n \t [4] DIVISION” ); 
     printf(“\n \t [5] REMAINDER” ); 
     printf(“\n \t [6] LARGER OUT OF TWO” ); 
     printf(“\n \t [7] EXIT” ); 
     printf(“\n \t = = = = = = = = = =”); 
     printf(“ \n\n\t ENTER YOUR CHOICE:”); 
       scanf(“%d”, &ch); 
       if(ch <= 6 && ch >=1)
         { 
          printf(“ENTER TWO NUMBERS:”); 
           scanf(“%d %d”, &a, &b); 
          } 
         switch(ch) 
          {  
	   case 1: c = a+b ; 
          printf(“ \n Addition: %d”, c); 
         break; 
          case 2: c=a-b; 
         printf(“\n Subtraction: %d”, c); 
         break; 
          case 3: c = a* b ; 
     printf(“\n Multiplication: %d”, c); 
         break;             
          case 4: c = a / b; 
     printf(“\n Division: %d”, c); 
        break; 
          case 5: c = a % b; 
     printf(“ \n Remainder: %d”, c);
     break; 
         case 6: if (a > b) 
     printf(“\n \t %d is larger than %d”, a, b); 
       else if (b > a) 
     printf(“ \n \t %d is larger than %d ”, b, a); 
        else 
     printf(“\n \t %d and %d are same”, a, b); 
     break; 
     case 7:  printf( “ \ n Terminated by choice”); 
     exit( ); 
     break; 
     default: printf(“ \ n invalid choice”); 
      } 
     getch ( );
}

  
Output :

= = = = = = = = =
      MENU
= = = = = = = = =
[1] ADDITION
[2] SUBTRACTION
[3] MULTIPLICATION
[4] DIVISION
[5] REMAINDER
[6] LARGER OUT OF TWO
[7] EXIT
= = = = = = = = = = = = = = =
Enter your choice: 6
Enter two numbers: 8 9
9 is larger than 8

  Program: Write a program to display the traffic control signal lights.
# include<stdio.h> 
# include<conio.h> 
main( ) 
{ 
    char L; 
    clrscr( );
    printf(“ \n Enter your Choice( R,r,G,g,Y,y):”); 
    scanf(“%c”, &L);  
    switch(L)
    { 
    case “R” :
    case   “r”: printf(“RED Light Please STOP”); 
    break; 
    case “Y” :
    case  “y”: printf(“YELLOW Light Please Check and Go”); 
    break; 
    case “G”:
    case  “g”: printf(“GREEN Light Please GO”); 
    break; 
    default: printf(“THERE IS NO SIGNAL POINT ”); 
    } 
  getch( ); 
 } 

Output :

Run-1:
  Enter your Choice(R, r, G, g, Y, y): g
  GREEN Light Please GO
Run-2:
  Enter your Choice(R, r, G, g, Y, y): R
  RED Light Please STOP