Refresh
Refresh
CONTROL STRUCTURES
• In our day to day life, we need to prepare ourselves for
any kind of situation thats going to happen so that a
proper output is obtained
the if statement
the if-else statement
the conditional operators
The if statement
• “if “ is a keyword
• General Form :
if (this condition is true)
execute this statement ;
• The condition following the keyword if is
always enclosed within a pair of
parentheses.
if conditions using relational
operators
• The relational
operators allow us to
compare two values to
see whether they are
equal to each other,
whether one is greater
than the other.
• = is for assignment
• == is for comparison
Sample program - 1
/* Demonstration of if statement */
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Sample program - 2
/* Calculation of total expenses */
main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
Is the statement dis = 0 necessary? The answer
is yes, since in C, a variable if not specifically
initialized contains some unpredictable value
(garbage value).
if conditions using arithmetic
operators
if ( 3 + 2 % 5 ) // mod operator has the higher priority
printf ( "This works" ) ;
if ( a = 10 ) // if (a) or if (10)
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
How ???
In C a non-zero value is considered to be true,
whereas a 0 is considered to be false.
Multiple Statements within if
main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and year of joining " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}
}
The if-else Statement
Can we execute one group of statements if the
expression evaluates to true and another group
of statements if the expression evaluates to
false?
Of course! This is what is the purpose of the
else statement .
Sample program
/* 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 ) ;
}
A point worth noting...
• General form
(expression1?expression 2:expression 3)
• Y=(4>5? 3 : 4)
Loop control structure
• The versatility of the computer lies in its
ability to perform a set of instructions
repeatedly .
• 3 types
– While loop
– For loop
– Do while loop
While loop
• General form
Initialise loop counter;
while(test loop counter using a condition)
{
Do this;
And this;
Increment (or) decrement the loop counter;
}
Example
count=1;
while(count<=3)
{
printf(“Fuzzy\n”);
count=count+1;
}
Output :-
Fuzzy
Fuzzy
Fuzzy
Indefinite loop
{
int i=1;
while(i<=10)
printf(“%d\n”,i);
} // no increment / decrement counter
Guess the output
int i=1;
while(i<=32767)
{
printf(“%d\n”,i);
i=i+1;
}
Hint : – Integer range is -32768 to 32767
And yes! It is an indefinite loop
A point to note
• Post-incrementation • Pre – incrementation
int i=0; int i=0;
while(i++<10) while(++i<10)
printf(“%d”,i) printf(“%d”,i)
Output :- Output :-
1 2 3 4 5 6 7 8 9 10 123456789
for loop
• General form