Conditional Statement 1
Logical Expressions n Uses relational and logical operators n Informally, specifies a condition which can be true or false n Evaluates to value 0 or 1 0 implies the condition is false 1 implies the condition is true 2
Logical Expressions (count <= 100) ((math+phys+chem)/3 >= 60) ((sex == M ) && (age >= 21)) ((marks >= 80) && (marks < 90)) ((balance > 5000) (no_of_trans > 25)) (! (grade == A )) 3
Relational Operators n Used to compare two quantities. < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to!= is not equal to 4
Examples 10 > 20 is false, so value is 0 25 < 35.5 is true, so value is 1 12 > (7 + 5) is false, so value is 0 32!= 21 is true, so value is 1 n When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared a + b > c d is the same as (a+b) > (c+d) 5
Logical Operators Logical AND (&&) n Evalutes to 1 if both the operands are non-zero Logical OR ( ) n Result is true if at least one of the operands is non-zero X Y X && Y X Y 0 0 0 0 0 non-0 0 non-0 non-0 0 0 non-0 non-0 non-0 non-0 non-0 6
Contd n Unary negation operator (!) Single operand Value is 0 if operand is non-zero Value is 1 if operand is 0 7
Example n (4 > 3) && (100!= 200) 4 > 3 is true, so value 1 100!= 200 is true so value 1 Both operands 1 for &&, so final value 1 n (!10) && (10 + 20!= 200) 10 is non-0, so value!10 is 0 10 + 20!= 200 is true so value 1 Both operands NOT 1 for &&, so final value 0 n (!10) (10 + 20!= 200) Same as above, but at least one value non-0, so final value 1 8
n a = 3 && b = 4 No parenthesis, so need to look at precedence and associativity = has higher precedence than && b=4 is an assignment expression, evaluates to 4 a = 3 is an assignment expression, evaluates to 3 Both operands of && are non-0, so final value of the logical expression is 1 n Note that changing to b = 0 would have made the final value 0 9
Example: Use of Logical Expressions void main () { int i, j; scanf( %d%d,&i,&j); printf ( %d AND %d = %d, %d OR %d=%d\n, i,j,i&&j, i,j, i j) ; If 3 and 0 are entered from keyboard, output will be 3 AND 0 = 0, 3 OR 0 = 1 10
Conditional Statements n Allow different sets of instructions to be executed depending on truth or falsity of a logical condition n Also called Branching n How do we specify conditions? Using expressions n non-zero value means condition is true n value 0 means condition is false Usually logical expressions, but can be any expression n The value of the expression will be used 11
Branching: if Statement if (expression) statement; if (expression) { Block of statements; 12
Branching: if Statement if (expression) statement; if (expression) { Block of statements; The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed. 13
marks >= 40 true print Passed print Good luck false 14
marks >= 40 true print Passed print Good luck A decision can be made on any expression. zero - false nonzero - true false 15
marks >= 40 true print Passed print Good luck A decision can be made on any expression. zero - false nonzero - true false if (marks >= 40) { printf( Passed \n ); printf( Good luck\n ); printf ( End\n ) ; 16
Branching: if-else Statement if (expression) { Block of statements; else { Block of statements; if (expression) { Block of statements; else if (expression) { Block of statements; else { Block of statements; 17
Grade Computation void main() { int marks; scanf( %d, &marks); if (marks >= 80) printf ( A ) ; else if (marks >= 70) printf ( B ) ; else if (marks >= 60) printf ( C ) ; else printf ( Failed ) ; 18
Find the larger of two numbers START READ X, Y YES IS X>Y? NO OUTPUT X OUTPUT Y STOP STOP 19
Find the larger of two numbers YES OUTPUT X START READ X, Y IS X>Y? NO OUTPUT Y void main () { int x, y; scanf ( %d%d, &x, &y) ; if (x > y) printf ( %d\n, x); else printf ( %d\n, y); STOP STOP 20
Largest of three numbers START READ X, Y, Z YES IS X > Y? NO Max = X Max = Y YES OUTPUT Max IS Max > Z? NO OUTPUT Z STOP STOP 21
START READ X, Y, Z YES IS X > Y? NO Max = X Max = Y YES OUTPUT Max IS Max > Z? NO OUTPUT Z STOP STOP 22
Max = X YES YES START READ X, Y, Z IS X > Y? IS Max > Z? NO Max = Y NO void main () { int x, y, z, max; scanf ( %d%d%d,&x,&y,&z); if (x > y) max = x; else max = y; if (max > z) printf ( %d, max) ; else printf ( %d,z); OUTPUT Max OUTPUT Z STOP STOP 23
Another version void main() { int a,b,c; scanf ( %d%d%d, &a, &b, &c); if ((a >= b) && (a >= c)) printf ( \n The largest number is: %d, a); if ((b >= a) && (b >= c)) printf ( \n The largest number is: %d, b); if ((c >= a) && (c >= b)) printf ( \n The largest number is: %d, c); 24
Confusing Equality (==) and Assignment (=) Operators n Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false n Example: if ( paycode = 4 WRONG! ) Will always print the line printf( "You get a bonus!\n" ); 25
Nesting of if-else Structures n It is possible to nest if-else statements, one within another n All if statements may not be having the else part Confusion?? n Rule to be remembered: An else clause is associated with the closest preceding unmatched if 26
Dangling else problem if (exp1) if (exp2) stmta else stmtb if (exp1) { if (exp2) stmta else stmtb OR if (exp1) { if (exp2) stmta else stmtb? Which one is the correct interpretation? Give braces explicitly in your programs to match the else with the correct if to remove any ambiguity 27
More Examples if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else s3? if e1 if e2 s1 else s2 else s3 28
Answers if e1 s1 if e1 s1 else if e2 s2 else { if e2 s2 if e1 s1 if e1 s1 else if e2 s2 else { if e2 s2 else s3 else s3 if e1 if e2 s1 if e1 { if e2 s1 else s2 else s2 else s3 else s3 29