Decision making statement in c programming language with example

What is decision making statement?

The decision making statements alter the normal sequential execution of the statement of the program depending upon the test condition to be carried out at a particular point in the program. The decision to be taken regarding where the control should transfer depends upon the outcome of the test condition. The following decision control statements are supported by C.
(a) if statement
(b) switch statement

(a). if statement:

The basic decision making statement used in most C programs is the if statement. The different types of if statements are:
1. Simple if statement                                                          2. if-else statement
3. Nested if-else                                                                    4. else-if ladder

1. SIMPLE If  :    Simple if statement is the most powerful decision making statement which is used to control the sequence of execution of statements. It allows your program to execute a simple or a block of statements only if the condition specified as an expression evaluates to true . No statement(s) is executed if it evaluates to false
       The syntax of simple if statement is,
         if(expression)
         {
       statement(s);
         }
     next statement;

A program of simple if statements to display message 'student has passed in English' if the marks in a subject are 50 or greater than 50 , otherwise no action is taken?
#include<stdio.h>
#include<conio.h>
main()
{
int marks;
clrscr();
printf"enter marks of student in english:  ");
scanf("%d",&marks);
if(marks>=50)
printf("student has passed in english");
getch();
}

OUTPUT:
enter marks of student in english: 56
student has passed in english


   2.The if -else statement:  In case simple if statement, the block of statements are executed only when the specified condition represented as an expression evaluates to true. But if it evaluates to false, nothing is done and control is transferred to the next executable statement following the if block. So if you want to take alternative action(s) when the condition represented by an expression evaluates to false then use an if-else statement.
It consist of if keyword, followed by a statement or block of statements, then a keyword else followed by another statement or block of statements.
The syntax of if-else statement is :
if (expression)
{
statementA(s);
}
else
{
statementB(s);
}
next_statement;


A program of if-else statement to test whether a given number is odd or even?
#include<stdio.h>
#include<conio.h>
main()
{
int num, rem;
clrscr();
printf("enter a number :");
scanf("%d",&num);
rem=num%2;
if(rem==0)
printf("number %d is even", num);
else
printf("number %d is odd", num);
getch();
}


OUTPUT:
enter a number  :15
number 15 is odd


3.Nested if -else statement: In the if-else statement, the body of if block is consist of a set of statement(s). It is also possible that an entire if-else statement or another if statement can occur either within the of  if statement or/and in body of else statement. This is known as nested if statement.
The syntax of nested if is,
if(condition1)
{
if (condition2)
statement1;
else
statement2;
}
else
{
if(condition3)
statement3;
else
statement4;
}

In this representations, we observe that nested if statement is used when a problem involves multiple test conditions and different actions are taken depending upon these test condition.

A program of nested if to calculate the greatest of three number:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("\nenter the three numbers :  ");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("a=%d is greatest",a);
else
printf("c=%d is greatest ",c);
}
else
{
if(b>c)
printf("b=%d is greatest",b);
else
printf("c=5d is greatest",c);
}
getch();
}

OUTPUT:
enter the three numbers : 10 20 30 
c= 30 is greatest


4. Else-if ladder :  In the programs involving multiple test conditions, the nested if-else statements makes the program very difficulty to write and understand especially if they are nested more deeply because as the number of test conditions goes on increasing, care must be taken to match multiple if-else constructs. In order to solve this problem , there is another way to write the same statements using else-if ladder.
The syntax of else-if ladder is,
if(expression)
statement1;
else if(expression2)
statement2;
else if (expression3)
statement3;

  •  
  •  
  •  
  •  
else
default_statement;
next_statement;


The different conditions represented using expression in the else-if ladder are evaluated in the top to bottom order. The else-if ladder is most general way of writing a program involving multiple conditions.

A program of else-if ladder to display the name of the angle when the user inputs the a number reflecting an angle in degrees?
#include<stdio.h>
#include<conio.h>
main()
{
unsigned int angle;
clrscr();
printf("enter a number indicating angle degree :");
scanf("%d",&angle);
if(angle>0 && angle<90)
printf("angle %u (in degrees) is acute angle ", angle);
else if (angle==90)
printf("angle %u (in degrees) is a right angle",angle);
else if (angle>90 && angle<180)
printf("angle %u (in degrees) is an obtuse angle",angle);
else if (angle==180)
printf("angle %u (in degrees) is straight angle ", angle);
else if (angle>180 && angle<<360)
printf("angle %u(in degrees) is reflex angle", angle);
else
printf("enter value between 1 and 360");
getch();
}

OUTPUT:
enter a number indicating angle degree : 120
angle 120(in degrees) is an obtuse angle




(b.)The switch statement:

The switch statement is a multi-way decision making statement which select one of the alternatives based on a set of a fixed values for given expressions. The switch statement is mainly used to replace multiple if-else statements. The use of multiple if-else statement causes performance degradation as several conditions need to be evaluated before a particular condition is satisfied.
The syntax of switch statement is,

switch(expression)
{
case constant1 : statement(s);
                            [break;]
case  constant2 : statement(s);
                              [break;]
................................................
default  : statement(s) ;
}
  
The switch statement starts with keyword switch followed by expression enclosed in the parentheses .


Program prompts user to enter the day of the week and then display is corresponding week day? For instance, display 'sunday' for 1, 'monday' for2, 'tuesday' for3 and so on.

#include<stdio.h>
#include<conio.h>
main();
{
int day;
clrscr();
printf("enter the day of week (1 to 7) : ");
switch (day)
{
case 1 : printf("today is SUNDAY"); break;
case 2 : printf("today is MONDAY"); break;
case 3 : printf("today is TUESDAY"); break;
case 4 : printf("today is WEDNESDAY"); break;
case 5 : printf("today is THURSDAY"); break;
case 6 : printf("today is FRIDAY"); break;
case 7 : printf("today is SATURDAY"); break;
default  : printf("enter a valid choice (1 to 7 only)");
}
getch();
}

OUTPUT:
enter the day of week (1 to 7) : 4
today is WEDNESDAY


                                          THANks







No comments

Powered by Blogger.