Decision Making Statements by G Krishna Chauhan


Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Type of Decision Making Statements:

  • if statement: The single if statement in C language is used to execute the code if condition is true.
The syntax of if statement is given below:

if(expression)
{  

//code to be executed  
}  

Flowchart of if statement in C:



Example Program:

#include<stdio.h>  
#include<conio.h>  
void main()
{  
int number=0;  
clrscr();  
printf("enter a number:");  
scanf("%d",&number);  
if(number%2==0){  
printf("%d is even number",number);  
}  
 getch();  
}  


Output:
enter a number:4
4 is even number
enter a number:5


  • if...else statement: An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.The syntax of if-else statement is given below:


if(expression)
{  
//code to be executed if condition is true  
}
else
{  
//code to be executed if condition is false  
}  

Flowchart of if ...else statement in C:

Example Program:

#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr();  
  
printf("enter a number:");  
scanf("%d",&number);  
  
if(number%2==0){  
printf("%d is even number",number);  
}  
else{  
printf("%d is odd number",number);  
}  
getch();  
}  

Output:
enter a number:4
4 is even number
enter a number:5
5 is odd number



  • If else-if ladder Statement: The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:

if(condition1)
{  

//code to be executed if condition1 is true  
}else if(condition2)
{  

//code to be executed if condition2 is true  
}  
else if(condition3)
{  

//code to be executed if condition3 is true  
}  
............................................
............................................

else
{  

//code to be executed if all the conditions are false  
}  


Flowchart of else-if ladder statement in C:




Example Program:


#include<stdio.h>  
#include<conio.h>  

void main(){  
int number=0;  
clrscr();  
printf("enter a number:");  
scanf("%d",&number);  
if(number==10)
{  

printf("number is equals to 10");  
}  
else if(number==50)
{  

printf("number is equal to 50");  
}  
else if(number==100)
{  

printf("number is equal to 100");  
}  
else
{  

printf("number is not equal to 10, 50 or 100");  
}  
getch();  
}

Output:

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50




  • nested if statements:If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.The syntax of switch statement in c language is given below:

if (condition1)
{
if (condition2)
 {
Statement1;
 }
else

{
Statement2; 
}
}
else
{
Statement3;}
      Example Program:

      #include<stdio.h>  
      #include<conio.h>  
      int main()
      {
      double n1, n2, n3;
      printf("Enter three numbers: ");
      scanf("%lf %lf %lf", &n1, &n2, &n3);
      if (n1>=n2)
      {
      if(n1>=n3)
      printf("%.2lf is the largest number.", n1);
      else
      printf("%.2lf is the largest number.", n3);
      }
      else
      {
      if(n2>=n3)
      printf("%.2lf is the largest number.", n2);
      else
      printf("%.2lf is the largest number.",n3);
      }
      return 0;
      }



  • switch statement: The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.

The syntax of switch statement in c language is given below:


switch(expression)
{    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
 default:     
 code to be executed if all cases are not matched;    
}    

Rules for switch statement in C language:


1) The switch expression must be of integer or character type.

2) The case value must be integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.

int x,y,z;  
char a,b;  
float f;

Valid Switch:switch(x),switch(x>y),switch(a+b-2),switch(func(x,y)).

Invalid Switch:
switch(f),switch(x+2.5).

Valid Case:case 3;case 'a';case 1+2;case 'x'>'y';

Invalid Case:
case 2.5;case x;case x+2;case 1,2,3;

Flowchart of switch statement in C:


Example Program:

#include<stdio.h>  
#include<conio.h>  
void main()
{  

int number=0;  
clrscr();  
printf("enter a number:");  
scanf("%d",&number);  
switch(number){  
case 10:  
printf("number is equals to 10");  
break;  
case 50:  
printf("number is equal to 50");  
break;  
case 100:  
printf("number is equal to 100");  
break;  
default:  
printf("number is not equal to 10, 50 or 100");  
}  
getch();  
}
Output:

enter a number:4

number is not equal to 10, 50 or 100


  • nested switch statements:You can use one switch statement inside another switch statement(s).The syntax of if statement is given below:


switch(ch1) 
{

case 'A': 
printf("This A is part of outer switch" );
switch(ch2) 
{
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}


Example Program:

#include <stdio.h>
#include <conio.h>
int main () 
{
  int a = 100;
   int b = 200;
  switch(a) 
{
   case 100: 
    printf("This is part of outer switch\n", a );
    switch(b) {
    case 200:
   printf("This is part of inner switch\n", a );
   }
   }
   printf("Exact value of a is : %d\n", a );
   printf("Exact value of b is : %d\n", b );
   return 0;
   }

Output:

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200



No comments:

Post a Comment