Presentation on theme: "Decision Structures and Boolean Logic"— Presentation transcript: 1 Decision Structures and Boolean Logic Show
2 Topics The if Statement The if-else Statement Comparing Strings
3 The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that
execute in the order they appear Decision structure: specific action(s) performed only if a condition exists Also known as selection structure 4 The if Statement (cont’d.) 5 The if Statement (cont’d.) 6 The if Statement (cont’d.) 7 Boolean Expressions and Relational Operators 8
Boolean Expressions and Relational Operators (cont’d.)
9 Boolean Expressions and Relational Operators (cont’d.)
10 Boolean Expressions and Relational Operators (cont’d.)
11 Boolean Expressions and Relational Operators (cont’d.) 12 The if-else
Statement Dual alternative decision structure: two possible paths of execution One is taken if the condition is true, and the other if the condition is false Syntax: if condition: statements else: other statements if clause and else clause must be aligned Statements must be consistently indented
13 The if-else Statement (cont’d.) 14 The if-else Statement (cont’d.)
15 Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings
can be compared using >, <, >=, and <= Compared character by character based on the ASCII values for each character If shorter word is substring of longer word, longer word is greater than shorter word 16 Comparing Strings (cont’d.)
17 Nested Decision Structures and the if-elif-else Statement 18
19 Nested Decision Structures and the if-elif-else Statement (cont’d.) 20 The if-elif-else Statement 21 The if-elif-else Statement
(cont’d.) 22 23 Logical Operators Logical operators:
operators that can be used to create complex Boolean expressions and operator and or operator: binary operators, connect two Boolean expressions into a compound Boolean expression not operator: unary operator, reverses the truth of its Boolean operand
24 The and Operator Takes two Boolean expressions as operands 25 The or
Operator Takes two Boolean expressions as operands 26 Short-Circuit Evaluation 27 The not Operator Takes one Boolean
expressions as operand and reverses its logical value Sometimes it may be necessary to place parentheses around an expression to clarify to what you are applying the not operator Truth table for the not operator Value of the Expression Expression false true
28 Checking Numeric Ranges with Logical Operators 29 Boolean Variables Boolean variable: references one of two values, True or
False Represented by bool data type Commonly used as flags Flag: variable that signals when some condition exists in a program Flag set to False condition does not exist Flag set to True condition exists
30 Summary This chapter covered: Decision structures, including: What Boolean variable signals when some condition exists in the program? A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist.
What type of operator determines whether a specific relationship exists between two values?A relational operator determines whether a specific relationship exists between two values. Concept: An if-else statement will execute one block of statements if its condition is true, or another block if its condition is false.
What executes statements only when a certain condition is met?Decision structure executes a set of statements under certain conditions.
What is a Boolean variable quizlet?Terms in this set (6)
Boolean variable. A variable having only two possible values, such as True/False.
|