Download presentation
Presentation is loading. Please wait.
1
Lecture 9: Implementing Complex Logic
2
Simple conditions Simple conditions using the relational operators:
>, <, >=, <= and equality operators: ==, != How to test multiple conditions in the process of making a decision? Nested if or if…else statements. Examples: If the gender is female and the age is no less than 65, then increament the count of the senior Females If one’s semester average is no less than 90 or his/her final exam is no less than 90, print “The student’s grade is A.” if (gender == 1) if (age >= 65) ++seniorFemales; if (semesterAverage >= 90) printf(“The student’s grade is A\n”); else if (finalExam >= 90)
3
Logical Operators Nonzero values are true, zero values are false
Logical operators used to form more complex conditions by combining simple conditions. Logical AND - && Return true if both conditions are true Logical OR - || Return true if either of its conditions are true Logical NOT - ! (logical negation) Reverses the truth/falsity of its condition Unary operator, has one operand Useful as conditions in loops Expression Result true && false false true || false true !false true Nonzero values are true, zero values are false
4
Logical Operators Examples:
If gender is female and the age is no less than 65, then increament the count of the senior Females If one’s semester average is no less than 90 or his/her final exam is no less than 90, print “The student’s grade is A.” Compute the largest number of user entered three numbers. if (gender == 1 && age >= 65) ++seniorFemales; if (semesterAverage >= 90 || finalExam >= 90) printf(“The student’s grade is A\n”);
5
Logical Operators Examples:
Compute the largest number of user entered three numbers. Pseudocode Input three numbers: num1, num2, and num3; If num1 >= num2 and num1 >= num3, Print num1 is the largest one. if num2 >= num1 and num2 >= num3, Print num2 is the largest one. if num3 >= num1 and num3 >= num1, Print num3 is the largest one.
6
Truth table for the && (logical AND) operator
7
Truth table for the logical OR (||) operator.
8
Truth table for operator ! (logical negation).
9
Operator precedence and associativity.
In expressions using operator &&, make the condition that is most likely to be false the leftmost condition. In expressions using operator ||, make the condition that is most likely to be true the leftmost condition. This can reduce a program’s execution time. Operator precedence and associativity.
10
Equality (==) and Assignment (=) Operators
Swapping both operators does not ordinarily cause syntax errors. Any expression that produces a value can be used in control structures Example of swapping == and = if ( payCode == 4 ) printf( “You get a bonus!\n” ); Checks payCode, if it is 4 then a bonus is awarded. Replacing == with =: if ( payCode = 4 ) Bonus awarded no matter what the payCode was.
11
lvalues and rvalues Variable names are said to be lvalues (“left values”) Used on the left side of an assignment operator Constants are said to be rvalues (“right values”) Used on only the right side of an assignment operator Lvalues can also be used as rvalues, but not vice versa. Only in assignment expressions, distinguish lvalues from rvalues. x =1 1 = x x == 1 1 == x
12
In-Class Programming Exercise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.