Introduction To Computers and Programming: Let Us C by Yashavant Kanetkar
Introduction To Computers and Programming: Let Us C by Yashavant Kanetkar
Introduction To Computers and Programming: Let Us C by Yashavant Kanetkar
Selection/Decision Control
The if statement The if-else Statement Decision depend upon conditions
The conditional operators o Equality o Relational
The if Statement
C uses the keyword if to implement the decision
control instruction.
if ( expression/this condition is true )
operators.
The relational operators allow us to compare two
values.
Relational Operators:
is A>B
Print A
Print B
Demonstration of if statement
/* 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 !" ) ; }
Examples:
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 ) ; }
Quick Test
if ( 3 + 2 % 5 ) printf ( "This works" ) ; if ( a = 10 ) printf ( "Even this works" ) ; if ( -5 ) printf ( "Surprisingly even this works" ) ; Non zero is true, zero is false.
Multiple IF Statements:
Compound Statements
Example:
The current year and the year in which the
employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.
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 ) ; } }
Pseudo code:
If students grade is greater than or equal to 60
Print failed
Code
If(grade >= 60) Printf(passed); else Printf(passed);
Example:
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.
Ternary Condition:
The conditional operators ? and : are sometimes
then the value returned will be expression 2, otherwise the value returned will be expression 3.
Nested if-else:
One inside another, test for multiple cases Once condition met, other statements skipped. Pseudo code:
If students grade is greater than or equal to 90 Print A else
Forms of If;
Logical Operators:
&& (Logical AND) True if both conditions are true
Sample Program:
To calculate the division
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 Nested if-else Logical operators
Solution:
if ( per >= 60 ) printf ( "First division ") ; else { if ( per >= 50 ) printf ( "Second division" ) ; else { if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "Fail" ) ; } }
if ( per >= 60 ) printf ( "First division ") ; if (( per >= 50 ) && (per < 60)) printf ( "Second division" ) ;
Else-if Statement
if ( per >= 60 ) printf ( "First division ") ; else if ( per >= 50 ) printf ( "Second division" ) ; else if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "Fail" ) ;
Contd:
Note that the else if clause is nothing different. It
is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code:
cases: If the driver is married If the driver is unmarried, male and above 30 years of age If the driver is unmarried, female and above 25 years of age.
if ( ms == 'M' ) printf ( "Driver is insured" ) ; else { if ( sex == 'M' ) { if ( age > 30 ) printf ( "Driver is insured" ) ; else printf ( "Driver is not insured" ) ; } else { if ( age > 25 ) printf ( "Driver is insured" ) ; else printf ( "Driver is not insured" ) ; } }
main( ) { char g ; int yos, qual, sal ; printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" ) ; scanf ( "%c%d%d", &g, &yos, &qual ) ; if ( g == 'm' && yos >= 10 && qual == 1 ) sal = 15000 ; else if ( ( g == 'm' && yos >= 10 && qual == 0 ) || ( g == 'm' && yos < 10 && qual == 1 ) ) sal = 10000 ; else if ( g == 'm' && yos < 10 && qual == 0 ) sal = 7000 ;
A Word of Caution
Contd:
main( )
{
int i ; printf ( "Enter value of i " ) ; scanf ( "%d", &i ) ; if ( i == 5 ) ; printf ( "You entered 5" ) ; }
End..