120 likes | 146 Views
Lecture 9: Implementing Complex Logic. 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:
E N D
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) printf(“The student’s grade is A\n”);
Logical Operators • 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
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”);
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.
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 ) printf( “You get a bonus!\n” ); Bonus awarded no matter what the payCode was.
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