The Decision Control Structure
The Decision Control Structure
The if Statement
Syntax:
if ( this condition is true )
execute this statement ;
The keyword if tells the compiler that what follows is a
decision control instruction.
The condition following the keyword if is always
enclosed within a pair of parentheses.
If the condition, whatever it is, is true, then the
statement is executed.
If the condition is not true then the statement is not executed;
instead the program skips past it.
Example:
/* Demonstration of if statement */ main( )
{
int num ;
if ( num <= 10 )
}
On execution of this program, if you type a number
less than or equal to 10, you get a message on the
screen through printf( ).
If you type some other number the program doesn’t
do anything.
}
The if-else Statement
The if statement by itself will execute a single
statement, or a group of statements, when the
expression following if evaluates to true.
Can we execute one group of statements if the
expression evaluates to true and another group of
statements if the expression evaluates to false?
Yes This is the purpose of the else statement
Example 2.3: In a company an employee is paid as
under:
If his basic salary is less than Rs. 1500, then HRA = 10%
of basic salary and DA = 90% of basic salary. If his
salary is either equal to or above Rs. 1500, then HRA =
Rs. 500 and DA = 98% of basic salary. If the employee's
salary is input through the keyboard write a program
to find his gross salary.
/* Calculation of gross salary */ main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ; scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ; da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
Nested if-elses
/* A quick demo of nested if-else */ main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ; scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
}
Use of Logical Operators
C allows usage of three logical operators, namely, &&,
|| and !.
These are to be read as ‘AND’ ‘OR’ and ‘NOT’
respectively.
Most obviously, two of them are composed of double
symbols: || and &&. Don’t use the single symbol |
and &. These single symbols also have a meaning
Example 2.4: The marks obtained by a student in 5
different subjects are input through the keyboard.
The student gets a
division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by
the student.
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5
);
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
}
This leads to three disadvantages:
a) As the number of conditions go on increasing the
level of indentation also goes on increasing. As a
result the whole
program creeps to the right.
b)Care needs to be exercised to match the corresponding
ifs and elses.
c) Care needs to be exercised to match the
corresponding pair of braces.
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5
);
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}
• from the second if statement, the && operator is
used to combine two conditions. ‘Second division’ gets
printed.
• if both the conditions evaluate to true. If one of the
conditions evaluate to false then the whole thing is
treated as false.
The else if Clause
The ! Operator
The third logical operator is the NOT operator,
written as !.
This operator reverses the result of the expression it
operates on.
For example, if the expression evaluates to a non-zero
value
The Conditional Operators
The conditional operators ? and : are sometimes
called ternary operators since they take three
arguments.
In fact, they form a kind of foreshortened if-then-else.
Their general form is,
expression 1 ? expression 2 : expression 3
int x, y ;
scanf ( "%d", &x ) ;
y=(x>5?3:4);
This statement will store 3 in y if x is greater than 5,
otherwise it will store 4 in y.
LOOP CONTROL STRUCTURE
This involves repeating some portion of the program
either a specified number of times or until a particular
condition I
Using a for statement
Using a while statement
Using a do-while statements being satisfied.
The while Loop
The general form of while is as shown below:
initialise loop counter ;
increment loop counter ;
while ( test loop counter using a condition )
{
do this ;
and this ;
}
The statements within the while loop would keep on
getting executed till the condition being tested
remains true.
When the condition becomes false, the control passes
to the first statement that follows the body of the
while loop.
The condition being tested may use relational or
logical operators
int i = 1 ;
{
main( )
{
while (i <= 10 )
printf ("%d\n", i ) ;
i=i+1;
}
}
The for Loop
The general form of for statement is as under: