Decision Control Statement

Condition control statements:-

C supports five types of condition control statements.

  • Simple if statement
  • If else statement
  • Nested if statement
  • Else if ladder
  • Switch statement

i) Simple if statement:- It is a decision making statements and is used to control the flow of execution.

Syntax
if(expression)
{
 statements;
}

If expression is true then the statement block will be executed and the statement block will be executed and the control transfer to the next statement.  Otherwise the expression is false then the control directly go to the next statement.

Program: Write a program to find max of two values by using simple if statement?

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
clrscr();
printf("Enter any two numbers:");
scanf("%d%d",&a,&b);
max=a;
if(max<b)
{
   max=b;
}
printf("Max value:%d",max);
getch();
}

Note:-  In any control statement the statement block contains only a single statement, braces are not necessary.

Program: Write a program to find the max value of given three numbers using simple if statements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("Enter any three numbers:");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("Max value:%d",max);
getch();
}

ii) If else statements:- It is an extension of simple if statement.

Syntax:- if(expression)
       {
            statement-1;
        }
         else
        {
    Statement-2;
         }

If the expression is true then if block statements are executed and else block statements are ignored.  Otherwise the expression is false, then else block statements are executed and if block statements are ignored.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
clrscr();
printf(“Enter any two numbers:”);
scanf(“%d%d”,&a,&b);
if(a<b)
max=a;
else
max=b;
printf(“Max value:%d”,max);
getch();
}

Program: Write a program to check whether the given no. is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“Given number is even”);
else
printf(“Given number is odd”);
getch();
}

iii) Nested if statement:- Using a if statement within another if is called nested if statement.  If a series of decisions are involved we use nested if statement.

Syntax:- 
if(expression-1)
{
   If(expression-2)
   {
     ------------
     ------------
    {
       Statements;
     }
   }
}

Syntax-2
if(expression-1)
{
if(expression-2)
{
statements-1;
}

else
{
statements-2;
}
}
else
{
if(expression-3)
{
statements-3;
}
else
{
statements-4;
}
}

Program: Write a program to find the maximum value of given 3 no’s using nested if.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf(“Enter any three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
max=a;
else
max=c;
}
else
{
if(b>c)
max=b;
else
max=c;
}
printf(“Max Value for given 3 numbers:%d”,max);
getch();
}

iv) else if ladder:- It is also used for a serried of decisions are involved.

Syntax:-if(expression-1)
{
stataement-1;
}
else if (expression-2)
{
statements-2;
}
 else if (expression-n)
 {
       statements-n;
   }
   else
    {
          statements;
     }

In this statement expression are evaluated top to bottom.  If the condition true, the statements associated that blocks is executed and the control transfer to the next statement.  When all expressions are false, then the final else block statements will be executed.

Program: Write a program to find the maximum value of given 3 no’s using nested if.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf(“Enter any three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
max=a;
else if(b>c)
max=b;
else
max=c;
printf(“Max Value for given 3 numbers:%d”,max);
getch();
}

v) Switch Statement:- It is a multi way conditional statement used in c language.  It is mainly used in situations where there is need to pick one alternate among many alternates.

Syntax:-
Switch (expression or variable)
{
Case value 1;
Statement1;
Break;
Case value n;
Statements n;
Break.;
Default;
Default statement;
}

The switch statement tests the value of the given variable or expression against a list of case values and when a match is found, a block of statements associated that case is executed.  Otherwise the default block statements will be executed.

Break:-  It is unconditional control statement and is used to terminate a switch statement.

Syntax :- break;

Note:- i) In switch statement the variable or expression is an integral value.(int & char).

ii) It cannot pass string and floating values.

iii) In switch statement the default block is optional.

Program: Write a program to test all arithmetic operations using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,opt;
clrscr();
printf("Enter any two numbers:");
scanf("%d%d",&a,&b);
printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Enter Option");
scanf("%d",&opt);
switch(opt)
{
case1 :
printf("\nAddition %d and %d is %d",a,b,a+b);
break;
case2 :
printf("\nSubtraction %d and %d is %d",a,b,a-b);
break;
case3:
printf("\nMultiplication %d and %d is %d",a,b,a*b);
break;
case4 :
printf("\nDivision %d and %d is %d",a,b,a-b);
break;
case5 :
printf("\nModulus %d and %d is %d",a,b,a%b);
break;
default :
printf("\nInvalid Region:");
}
getch();
}

Follow Us On