1 / 12

Lecture 9: Implementing Complex Logic

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:

hnorman
Download Presentation

Lecture 9: Implementing Complex Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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) printf(“The student’s grade is A\n”);

  3. 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

  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.

  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 ) printf( “You get a bonus!\n” ); 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

More Related