120 likes | 147 Vues
Learn about boolean operators like AND, OR, and NOT, and how to use sets in Pascal programming. This section includes examples and explanations.
E N D
We will not cover the following sections in class: • 7.2 • 7.4 • 7.8 (we will discuss the design topics) • You ARE responsible for knowing the topics covered in these sections. • The topics are covered in the other sections of the chapter. However, these sections contain very good examples and you should study them.
Boolean Operators: AND If (BoolEx1) AND (BoolEx2) THEN writeln(‘Both expressions are true’); When using the AND Operator, both expressions must be TRUE for the compound expression to be true.
Boolean Operators: OR If (BoolEx1) OR (BoolEx2) THEN writeln(‘At least one expression is true’); When using the OR Operator, at least one expression must be TRUE for the compound expression to be true.
Boolean Operators: NOT If NOT (BoolEx) THEN writeln(‘BoolEx is FALSE’); The NOT Operator simply reverses the boolean value of the boolean expression that appears immediately to its right. NOT (a <= 2) OR (b = 2) will evaluate to TRUE if a is greater than 2 and / or b =2.
Pascal Sets • In Pascal you can define sets by placing a group of ‘elements’ of any single ordinal type between brackets. • [‘A’, ‘E’, ‘I’, ‘O’, ‘U’] • [1..9] {all values between 1 and 9}
Pascal Sets: The IN operator • The boolean operator IN is used to test whether an item is an element of a set. FOR Letter := ‘A’ TO ‘Z’ DO IF Letter IN [‘A’, ‘E’, ‘I’, ‘O’, ‘U’] THEN write(letter);
Precedence Priority Operator(s) 1st NOT 2nd AND, *, /, DIV, MOD 3rd OR, +, - 4th =, <>, >, <, >=, <=, IN
Section 7.7 Skip for now we may revisit include files
Sections 7.8 Per first slide, we will discuss the design issues in another class. You should read and “grok” this section’s example
Nested IF Statements When you nest IF statements, an else will match to the closest ‘non-matched’ IF. IF Age >= 21 THEN IF Age <= 35 THEN writeln(‘come in’) ELSE writeln(‘Do you know this is a college bar’); The ELSE will match with IF AGE <= 35
Multiple Alternative IF-THEN-ELSE • The multiple IF-THEN-ELSE is a multiway switch that allows you to choose only one of many options. IF A < 10 THEN … ELSE IF A < 15 THEN … ELSE ...
Homework #6 • Due next Thursday, November 11 • On Marateck’s website homework #5 • The assignment is like a “version 2.0” of the one due today.