DECISION MAKING
STATEMENTS
• A program is nothing but the execution of sequence of one
or more instructions.
• Quite often, it is desirable to alter the sequence of the
statements in the program
• Depending upon certain circumstances / conditions. (i.e., we
have a number of situations where we may have to change
the order of execution of statements based on certain
conditions) (or)
• Repeat a group of statements until certain specified
conditions are met.
•C supports mainly three types of control
statements
I. Decision making statements
II. Loop control statements
III. Unconditional control
statements
I. Decision making statements
1) Simple if Statement
2) if – else Statement
3) Nested if-else statement
4) else – if Ladder
5) switch statement
IF - STATEMENT
1) Simple if Statement
2) if – else Statement
3) Nested if-else statement
4) else – if Ladder
1. Simple “if” statement:
•The if statement is a powerful decision
making statement.
• It is used to control the flow of execution
of statements.
Syntax:
if (Condition or test expression)
{
Statement block;
}
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 ie., ( )
•The Curly Braces indicates the scope of “if” statement.
• The default scope is one statement.
• The statement block may be a single statement or a
group of statements.
• If the Test Conditions is TRUE, the Statement Block
will be executed .
• If the Test Condition is FALSE, the Statement Block will be
skipped.
Flow chart for “ if ” statement:
#include<stdio.h>
void main( )
{
int a=10;
if(a < 20)
{
printf(“a is less than 20”);
}
getch();
}
Output:
a is less than 20.
2. if-else Statement:
• It has two blocks. (True block and False block)
•When the condition is true, True block will be
executed.
•When the condition is false, false block will be
executed.
•In this, 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
Write a program to print the given number is even or odd.
# include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\n 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:
Enter a number: 24
The given number is EVEN
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 Condition 1)
{
if ( Test Condition 2)
{ Statement -1; }
else
{ Statement -2; }
}
e
l
s Statement -3; /* False block for condition-1 */
}e
{
# include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
printf(“\n 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);
}
}
Output:
Enter three values: 9.12 5.34 3.87
Largest Value is: 9.12
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 if
statement is associated with each else.
•Hence it forms a ladder called else–if ladder
Syntax:
if ( Test Condition 1)
{
True block;
}
else
{
if ( Test Condition 2)
{ true Statement; }
else
{ false Statement;
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the numbers:”);
scanf(“%d %d %d”,&a,&b,&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); }
}
Output:
Enter the numbers: 12 34 10
Highest number is : 34