Decision Control Statement

In a C program ,a decision causes a one-time jump to a different part of a program , depending on the value of an expression.Decisions in C can be made in several ways.The most important is with the if..else statement,which chooses between two alternatives.This Statement can be used without the else ,as a simple if statement.Another decision control statement ,switch ,creates branches for multiple alternative sections of code, depending on the value of a single variable .


Types of Decision Control Statement :-

1) if statement

2)if...else statement

3)Nested if ... else statement

4)Else if statement

5)Switch statement


Simple if statement

It is used to execute an instruction or block of instructions only if a condition is fulfilled

The syntax is as follows:

if(condition)

{

 block of statements;

}



Example :

Write a program to calculate the net salary of an employee,if a tax of 15% is levied on his gross-salary if it exceeds Rs. 10,000 /- per month .

 #include<stdio.h>
 OUtput  main()
{
float gross_salary,net_salary;

printf("Enter gross salary of an employee\n");
scanf("%f",&gross_salary);

if(gross_salary<10000)
  net_salary=gross_salary;

if(gross_salary>=10000)
 net_salary=gross_salary-0.15*gross_salary;

printf("\n Net salary is Rs. %.2f\n",net_salary);
}

OUTPUT

Enter gross salary of an employee
9000
Net salary is Rs.9000.00

Enter gross salary of any employee
10000

Net Salary is Rs.8500.00

 
 If...else statement

if...else statement is used when a different sequence of instructions is to be executed  depending on the logical value (True/False) the condition evaluated.


The syntax is as follows:

if(condition)
{
statement_1_block ;
}
 else{

statement_2_block ;

}
Statement_3_block;



Example

Write a program to print whether the given  number is even or odd.

#include<stdio.h>

main()
{
int x;
 printf ("Enter a number :\n");
 scanf("%d",&x);
if(x%2==0)
printf("\n Given number is even\n");
else
printf("\n Given number is odd \n");
}

 OUTPUT
 Enter a number :
6
Given number is even
Enter a number:
7
Given number is odd

Nested if..else Statement

in nested if..else statement,an entire if..else construct is written within either the body of the if statement or the body of an else statement.the syntax is as follows:

The syntax is as follows:

if(condition1)
{
 if(condition2)
{
 Statement_1_block;
}
else{
Statement_2_block;
}
}
else
{
Statement_3_block;
}
Statement_4_block;


Example

Write a program to calculate an Air ticket fare after discount,given the following conditions:
1)If passenger is below 14 years then there is 50 %  discount on fare .
2)if passenger is above 50 years then there is 20 % discount on fare .
3)if passenger is above 14 and below 50 then there is 10 % discount on fare.


#include<stdio.h>
main()
{
int age;
float fare;
printf("\n Enter the age of passenger :\n");
scanf("%d",&age);
printf("\n Enter air ticket fare \n");
scanf("%f",&fare) ;
if(age<14)
fare=fare-0.5*fare;
else
if(age<=50)
{
fare=fare-0.1*fare;
}
else
{
fare=fare-0.2*fare;
}
printf("\n Air ticket fare to be charged after discount is %0.2f",fare);
}
 
OUTPUT

Enter the age of passengert
12
Enter the Air ticket fare
2000.00
Air ticket fare to be charged after discount is 1000.00

Else if statement 

To show a multi-way decision based on several conditions,we use the else if statement.This works by cascading of several comparisons.As soon as one of the conditions is true,the statement or block of statement following them is executed and no further comparisons are performed.The syntax is as follows:

if(condition_1)
{
Statement_1_block;
 }
else if
{
Statement_2_block;
}  

 
 .......
else if (Condition_n)
{

Statement_n_block;
}
 else
Statement_x;



Example

Write a program to award grades to students depending upon the criteria mentioned below:
1) Marks less than or equal to 50 are given "D" grade.
2)Marks above 50 but below 60 are given "C" grade.
3)Marks above 60 to 75 are given "B" grade.
4)Marks above 75 are given "A" grade.


#include<stdio.h>
main()
{
int result;
printf("Enter total marks of a student:\n") ;
scanf("%d",&result);
if(result<=50)
printf("Grade D \n");
else if (result<=60)
printf("Grade C \n");
else if (result<=75)
printf("Grade B\n");
else
printf("Grade A"\n);
}

OUTPUT 
Enter the total marks of a student:
80
Grade A

 
Switch statement

Its objective is to check several possible constant values for an expression,something similar to what we had studied in the earlier sections,with the linking of several if and else if statements.When the actions to be taken depending on the  value of control variable ,are large in number,then the use of control structure Nested ....if else makes the program complex .There switch statement can be used.

Syntex is as follows:

switch(expression){
case expression 1 :
block of instructions 1
break;
case expression 2:
bock of instructions 2
break;
..
case expression n:
block of instruction n
break;

default:
default block of instructions 

}


Example

Write a  program that performs the following ,depending upon the choice selected by the user .
1)calculate the square of number if choice is 1.
2)calculate the square rooot of number if choice is 2 and 4 .
3)calculate the cube of given number if the choice is 3.
4)otherwise print the number as it is .


main()
{

int choice,n;
printf("\n nter any number :\n");
scanf("%d",&n) ;
printf("choice is as follows:\n\n");
printf("1.To find the square of a number\n");
printf("2.To find the square-root of a number\n");
printf("3.To find the cube of a number\n");
printf("4.To find the square-root of a number\n\n");;

printf("Enter your choice\n");
scanf("%d",&choice) ;
switch(choice)
{
case 1 :  printf("The square of a given  number is %d\n" ,n*n);
break;
case 2:printf("The square-root of a given number is %f \n" ,sqrt(n));
break;
case  3 : printf("The cube of a given number is %d\n" ,n*n*n);
 break;
case 4 :printf("The square-root of a given number is %f \n" ,sqrt(n));
break;
default: printf("The given number is %d\n" ,n);
break;


}
}

OUTPUT

Enter any number :
4
Choice as follows :
1.To find the square of a number .
2.To find the square root of a number .
3.To find cube of a number .
4.To find the square-root of a number
Enter your choice :
2
The square-root of  given number is 2

0 comments:

Post a Comment