Conditions, logical expressions, and selection Introduction to control structures 1
Flow of control In a program, statements execute in a particular order By default, statements are executed sequentially: One after another, from top to bottom One at a time Exactly once This sequential flow of control can be altered through the use of control structures 2
Control structures There are two general types of control structures: Selection (aka branching) structures: cause flow of control to take, or not take, a particular direction Repetition (aka iteration, looping) structures: cause a set of statements to execute several times Both of these type of structures depend on the evaluation of logical expressions 3
Flow charts for control structures Selection Repetition 4
Logical expressions Logical expressions evaluate to true or false Most logical expressions use relational and logical operators In App Inventor, as in most programming languages, the form of a simple logical expression is: Operand1 operator Operand2 The operands can be simple or compound expressions of any type The operator is a relational operator 5
Relational operators The relational operators in AI (found in the Math drawer) include: < : is less than <= : is less than or equal to = : equals > : is greater than >= : is greater than or equal to not= : does not equal 6
Logical expression examples Suppose you had the following declarations: x = 3, y = 7; Then the following expressions would have the values indicated: x > y // (false) y >= x // (true) x not= y // (true) (x > y) = true // (false) ((y >= x) = (x not= y)) // true 7
Logical operators Three operators in AI (found in the Logic drawer) can be used to form compound logical expressions They are: and - logical and or - logical or not logical not 8
Logical operators Logical and (&&) combines two expressions; if both sub-expressions are true, then the compound expression is true otherwise, the compound expression is false Logical or also combines two expressions; the compound expression is true if one or both subexpressions is true, false otherwise Logical not reverses the truth value of an expression; if the original expression was true, not makes it false, and vice versa 9
Truth table Graphical display of relationships between truth values of propositions Shows all possible values of propositions, or combinations of propositions Suppose p represents an expression; then the truth table for!p is as show below: p not p T F F T 10
What is the value? Given: age = 25; height = 70; EXPRESSION VALUE not (age < 10)? not (height > 60)? 11
Truth table for p and q Suppose p and q represent two logical sub-expressions; then the compound expression p and q has the following truth table: p q p and q T T T T F F F T F F F F 12
Truth table for p or q Suppose p and q represent two logical sub-expressions; then the compound expression p or q has the following truth table: p q p or q T T T T F T F T T F F F 13
age = 20; temperature = 102.0; issenior = (age >= 55) ; hasfever = (temperature > 98.6) ; EXPRESSION issenior and hasfever issenior or hasfever not issenior not hasfever VALUE false true true false 14
Logical equivalence Two different logical expressions that yield the same result are said to be logically equivalent Logical equivalence is not always obvious; two examples follow that illustrate this 15
Example 1 We are given the following two expressions: not((a=b) or (A=C)) (A not= B) and (A not= C) Are they equivalent? To find out, create truth tables 16
Truth Tables not((a=b) or (A=C)) (A not= B) and (A not= C) A B C A=B A = C or not 0 0 0 1 1 1 0 0 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 A B C A not= B A not= C and 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1 0 0 0 17
Example 2 not (A and B) A B A and B not 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 A or B A B A or B 0 0 0 0 1 1 1 0 1 1 1 1 18
Short-Circuit Evaluation Many programming languages use short circuit evaluation of logical expressions This means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined 19
Short-Circuit Example Given: age = 25; height = 70; EXPRESSION (age > 50) and (height > 60) false Evaluation can stop now because result of and is only true when both sides are true. It is already determined that the entire expression will be false. 20
More Short-Circuiting Given: age = 25; height = 70; EXPRESSION (height > 60) or (age > 40) true Evaluation can stop now because result of or is true if one side is true. It is already determined that the entire expression will be true. 21
What happens? Given: age = 25; weight = 145; EXPRESSION (weight < 180) and (age >= 20) true Must still be evaluated because truth value of entire expression is not yet known. Why? Result of and is only true if both sides are true. 22
What happens? Given: age = 25; height = 70; EXPRESSION! (height > 60) or (age > 50) true false Does this part need to be evaluated? 23
Write an expression for each taxrate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22 24
Logical expressions & program control Relational expressions can be used to control the flow of logic in a program Depending on the truth value of an expression, a program can be made to perform one task or another (but not both) A control structure that fits this description is called a selection structure 25
Logical expressions & program control Relational expressions can be used to control the flow of logic in a program Depending on the truth value of an expression, a program can be made to perform one task or another (but not both) A control structure that fits this description is called a selection structure 26
Selection Blocks in AI Simple if block 27
Compound Selection in AI Nested structure 28
Compound Selection Example Suppose you need to design the software for a controller that monitors the temperature of a critical piece of equipment If the temperature falls within the normal range (55-80 F), no action is necessary But if the temperature falls below 55 or rises above 80, an alarm should be triggered: 10 degrees in either direction should sound the immediate action alert More than 10 degrees in either direction should trigger an automatic shutoff 29
Example continued Variable to test: temp Expressions: Normal range: temp >= 50 and temp <= 80 Action alert: (temp<50 and temp>40) or (temp>80 and temp<90) Automatic shutoff: temp <= 40 or temp >= 90 30
Flowchart (not a very good one!) 31