Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views42 pages

Introduction To Computers and Programming: Let Us C by Yashavant Kanetkar

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 42

Introduction to Computers and Programming

Let Us C by Yashavant Kanetkar Chapter 2

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 )

execute this statement ;


We express a condition using Cs relational

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.

/* 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 ) ; }

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

Set of statements within a pair of braces.


BLOCK:
Set of statements within braces.

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 ) ; } }

The if-else Statement


If/else
Different actions if conditions true or false.

Pseudo code:
If students grade is greater than or equal to 60

Print passed else

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.

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 ) ; }

Ternary Condition:
The conditional operators ? and : are sometimes

called ternary operators since they take three arguments.


expression 1 ? expression 2 : expression 3 if expression 1 is true (that is, if its value is non-zero),

then the value returned will be expression 2, otherwise the value returned will be expression 3.

Ternary conditional operator (? :)

Three arguments (condition, value if true, value if false)

Code could be written:

(grade >= 60 ? PASSED : FAILED);

1) char a ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;

2) int x, y ; scanf ( "%d", &x ) ; y=(x>5?3:4);

if ( x > 5 ) y=3; else y=4;

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

If students grade is greater than or equal to 80


Print B else If students grade is greater than or equal to 70 Print C else If students grade is greater than or equal to 60 Print D else Print F

Forms of If;

Logical Operators:
&& (Logical AND) True if both conditions are true

If (gender == F && age >= 65) Senior Females;


|| (Logical OR) True if either of condition is true

If ( semesterAverage >= 90 || finalexam >= 90) printf(student grade is A);


! (Logical NOT, logical negation) Returns true when its condition is false, & vice versa

If ( ! (colour == red ) ) printf(its not a bright colour);

Sample Program:
To calculate the division

Input: marks of 2 different subjects


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 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" ) ;

if (( per >= 40 ) && (per < 50)) printf ( "Third division" ) ;


if ( per < 40 ) printf ( "Fail" ) ;

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:

Using Logical Operators


A company insures its drivers in the following

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" ) ; } }

Salary Calculation Example:


Write a program to calculate the salary as per the following table:

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 ;

else if ( g == 'f' && yos >= 10 && qual == 1 )


sal = 12000 ; else if ( g == 'f' && yos >= 10 && qual == 0 ) sal = 9000 ; else if ( g == 'f' && yos < 10 && qual == 1 ) sal = 10000 ; else if ( g == 'f' && yos < 10 && qual == 0 ) sal = 6000 ; printf ( "\nSalary of Employee = %d", sal ) ; }

Hierarchy of Operators Revisited

A Word of Caution

Contd:
main( )

{
int i ; printf ( "Enter value of i " ) ; scanf ( "%d", &i ) ; if ( i == 5 ) ; printf ( "You entered 5" ) ; }

End..

You might also like