Download presentation
Presentation is loading. Please wait.
Published byLeona Small Modified over 6 years ago
1
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Boolean Expressions Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
2
True or False Boolean expressions are named after George Boole.
A boolean expression has only two possible values – usually thought of as true or false. Boolean expressions are sometimes referred to as conditional expressions.
3
Recall Comparison Operators
Pascal English Example = equal to Ans = ‘Y’ <> not equal to x <> y < less than z < 5 <= less than or equal to z <= 10 > greater than q > -9 >= greater than or equal to q >= 12
4
Recall Comparison Ops 2 Comparison Operators Result in Boolean Expressions. So the expression: (x < 2) is a boolean expression which EVALUATES to true or false.
5
Another ‘comparision’
Say we had a set of values: { 1, 4, 6, 11, 21, 38 } and we wanted to know if the variable x was equal to any of those values. We could ask a lot of questions like: is x = 1 is x = 4 is x = 6 and so on until we got to is x = 38 But that would take 6 questions.
6
Another ‘comparision’ 2
So rather than ask 6 questions we would like to ask just one: Is x in the set { 1, 4, 6, 11, 21, 38 } Pascal lets us do this by saying: ( x IN [1, 4, 6, 11, 21, 38 ] ) And if x’s value is one of those values the above expression would evaluate to be true. Note Pascal only allows character sets or integer sets containing the values from 0 to 255.
7
And, or, not The reserved words AND, OR, NOT have special meanings when used in Pascal. They allow us to create complex boolean expressions.
8
NOT Not is used to negate a boolean expression.
For example you might say: not (x < 2) Which would evaluate the same as: (x >= 2)
9
AND Loosely speaking, AND is used to require two boolean expressions to both be true. For example if you wanted to know if x is between 0 and 5 you could say: (x > 0) AND (x < 5)
10
OR Loosely speaking, OR is used to require one condition OR another to be true OR both. For example if you wanted to know if x is less than 3 OR greater than 10 you could say: (x < 3) OR (x > 10)
11
NOT – truth table Expression Value not (true) false not (false) true
12
AND – truth table Expression Value true AND true true true AND false
false AND true false AND false
13
OR – truth table Expression Value true OR true true true OR false
false OR true false OR false false
14
Where to use boolean expressions
Most often you will use boolean expressions in if-then-else (conditional) statements or in loops.
15
Complex Boolean In Pascal we are allowed to mix up and create very complicated boolean expressions. For example we could say: if ( NOT (x < 3) AND (y > 4) ) OR ( x < 3) AND ( y <= 4) then But we should try to make them as easy to evaluate as possible.
16
Examples of complex boolean expressions
Refer to the book (or presentation in class) for examples of complex boolean expressions.
17
Short Circuit Evaluation
Turbo Pascal defaults to short circuit evaluation of boolean (conditional) expressions. This means that in a program as soon as a determining expression is evaluated in a complex expression, the evaluation of the complex expression stops.
18
Short Circuit Example Pretend x = 10 and y = 7
Consider the below statement: if (x > 12) AND (y > 5) then As soon as the (x > 12) expression is evaluated to false we know that the complex expression (x > 12) AND (y > 5) MUST be false (because false AND [anything] is false).
19
Short Circuit Evaluation 2
Short circuit evaluation can be used to do ‘clever’ things in a program. Good programmers will NOT rely on short circuit evaluation to do anything. Doing so makes programs difficult to read and maintain and further relies on the assumption that short circuit evaluation will always occur – which is NOT a valid assumption.
20
Short Circuit Evaluation What you need to know
Because not all programmers are good, you must be able to understand how a program would work if it was using short circuit evaluation and how the same program would work if it was not using short circuit evaluation. Note if short circuit evaluation is NOT used, then we say complete evaluation was.
21
Boolean types On a nice note, if you ever want to see the value of a boolean expression, Pascal allows you to declare variables of type boolean. These variables may have only two values: true or false. The program on page 208 offers a good example of how to use boolean variables.
22
Excercises (not graded)
I would recommend doing exercises: 1, 2, 3 on pages 206, 207 4, 5, 6, 7, 9, 10 on pages 211, 212
23
End of Boolean Expressions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.