4 If Else 28dec2020
4 If Else 28dec2020
4 If Else 28dec2020
Flow of Control
• Unless specified otherwise, the order of statement
execution through a function is linear: one
statement after another in sequence
• Some programming statements allow us to:
▪ decide whether or not to execute a particular statement
▪ execute a statement over and over, repetitively
if ( condition )
statement;
condition
evaluated
true
false
statement
Relational Operators
• A condition often uses one of C's equality
operators or relational operators
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
if ( condition )
statement1;
else
statement2;
condition
evaluated
true false
statement1 statement2
Practice
• Example 2.1: While purchasing certain items, a
discount of 10% is offered if the quantity
purchased is more than 1000. If quantity and price
per item are input through the keyboard, write a
program to calculate the 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 ) ;
}
Example: Wages.c
• Write a C program that calculates weekly wages
for hourly employees.
a !a
true false
false true
Logical AND and Logical OR
• The logical AND expression
a && b
is true if both a and b are true, and false otherwise
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
Boolean Expressions
• Specific expressions can be evaluated using truth
tables
total < MAX found !found total < MAX && !found
false false true false
false true false false
true false true true
true true false false
Boolean Expressions in C
• C does not have a boolean data type.