480 likes | 577 Views
Introduction to condition statements Menu. Learning Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page 2 Review of Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page 3
E N D
Introduction to condition statementsMenu Learning Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Page 2 Review of Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page 3 More About Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . .Page 4 More About Relational Expressions . . . . . . . . . . . . . . . . . Page 6 More About Logical Expressions . . . . . . . . . . . . . . . . . . . Page 8 More About Operator Precedence . . . . . . . . . . . . . . . . . . . Page 16
Introduction to Condition StatementsLearning objectives Upon completion of this lesson, the student will be able to: • Discuss and give examples and implement the following terms : • Flow of control • Sequential, selections, and control structures of program executions • Relational and logical operators. • Truth tables • Boolean variables • Simple and complex expressions • Compound conditional expressions • Evaluate and write a simple relational expression • Evaluate and write a compound relational expression
Introduction to Condition StatementsReview of flow control The flow of control in a program refers to the logical flow in which the program statements are executed. There are three basic control structures: sequential, selections, and repetition. Sequential : Sequential execution occurs when each statement is executed in order from first to last. The flow of control is sequential until one of the other control structures is indicated. Selections : Selections occurs when the program logic must choose which of one or more sets of instructions will be executed. This is also called branching. The path selected depends on the evaluation of a giving condition. If the condition is true, one set of statements is executed. If the condition is false, another set of statements is executed. The selection structure is presented in more detail in the IF statements Module.
Introduction to Condition StatementsMore about Boolean expressions George Boole was a mathematician of the 1800’s who invented a system of mathematics that had only two numbers; 0 and 1. In logic 1 corresponds to true and 0 corresponds to false. A Boolean expression evaluates to a value of either true or false. Example : When the variable x stores the value 3, the Boolean expression x < 8 is true. A Boolean value is the value of true or false. Variables of the Boolean data type can only store Boolean values. Example : Since the Boolean variable flag stores a Boolean value, flag = true is allowed; flag = ‘stop’ is not allowed, because ‘stop’ is not a Boolean value, it is a alphanumeric.
Introduction to Condition StatementsMore about Relational expressions To evaluate a condition statement is to decide its Boolean (or truth) value. Example : Evaluate x < = 10, for x = 5. This can be evaluated like this. Step 1. Replace x with its given value: 5< = 10 Step 2. Determine the truth value of this new expression: 5 < = 10 is true Step 3. The original x < = 10 is true for x = 5 Relational operators are used to construct simple relationalexpressions and logical operators are used to join simple relational expressions in order to construct compound conditional expressions. Relational operators : are = (equal to) > (greater than) < (less than) >= (greater than or equal to) <= (less than or equal to) <> (not equal) Simple relational expressions : For x = 3, x > 5 is a simple expression that is false. For x = 2, y = 8, x < y is a simple expression that is true
Introduction to Condition StatementsMore about Relational expressions Logical operators : NOT, AND, OR Compound conditional expressions : For x = 3, y = 8, (x < 4 and x < = y) is a compound expression that is true.
Introduction to Condition StatementsMore about Relational expressions Relational operators compare the relationship between the two given values of the same data type. Example : For x = 3, y = 7 , x < y is true.
Introduction to Condition StatementsMore about Logical Operators Logical operators from compound conditional expressions when they are used to join simple relational expressions. Logical operators : NOT, AND, OR Join : For x = 10 and y = 3 NOT (x < 10) evaluates to true X < 10 AND y > = 3 evaluates to false X < 10 OR y > = evaluates to true A compound conditional expression is evaluated as a single true of false statement. Conditional expressions : A conditional expression is a condition statement that uses the relational and logical operators to determine the Boolean value of the expression.
Introduction to Condition StatementsMore about Logical Operators Evaluation of compound conditional expressions is more complicated than evaluation of simple relational expressions. A truth table can be helpful when evaluating the compound expressions. Truth table : A truth table is a table specifying when ‘NOT’, ‘AND, and ‘OR’ statements evaluate to true or false. Concept : A truth table is a technique to determine whether an expression is valid or not. For instance, lets assume that you want to borrow you Dad’s car. He says that you can, if you mow the lawn and take out the trash. A truth table for that situation would look like this :
Introduction to Condition StatementsMore about Logical Operators Now what if dad said if you mowed the lawn or took out the trash? The truth table would look like this : The difference : AND requires both conditions to be met OR requires either condition to be met. The truth table for the expression NOT p is: The NOT expression evaluates to the opposite of the value generated by the expression.
Introduction to Condition StatementsMore about Logical Operators The truth table for the expression p AND q is : T : Solution r >= 3.14 AND r < 10.0: Step 1: 3.14 > = 3.14 AND 3.14 < 10.0 Step 2: T T Step 3: T AND expressions are true when all of the relational expressions in the statement are true. Example : If r = 6.4, the statement : r > = 3.14 AND r < 10.0 is true since both parts are true. This corresponds to row one of the table.
Introduction to Condition StatementsMore about Logical Operators The truth table for the complex expression p OR q looks like this : T : Solution : x < = 8 OR x > 10, for x = 7: Step 1: 7 < = 8 OR 7 > 10 Step 2: T F Step 3: T OR expressions are true when at least one of the relational expressions in the statement is true. Example: For x = 7, the statement x < = 8 OR x > 10 is true. This corresponds to row 2 of the truth table.
Introduction to Condition StatementsMore about Logical Operators A technique called short circuit evaluation can be used to evaluate compound relational expressions. And “OR” statement is true if one part is true. Evaluating and “OR” statement where the first part is true results in no need to look at the second part. The complete “OR” statement is true because one of the expressions is true. Likewise the “AND” statement is false when just one part is false. Evaluating an “AND” statement were the first part is false means the complete “AND” statement is false. short circuit evaluation : for x = 3 , and y = -4: The statement x < 5 OR y > = -2, is true since the first part x < 5 is true. The statement x < y AND y < = 7 is false since the first part x < y is false. Short circuit evolution is efficient because it saves computer time.
Introduction to Condition StatementsMore about operator precedence The precedence order for arithmetic operators was discussed in the Introduction to Control structures module. With the introduction of the relational and logical operators in this module, the combined order of operations is: • / % : Preformed first : It is important to remember that expressions enclosed in parenthesis are used to override the order of operations. When expressions appear in parenthesis, they are always evaluated first. • + - • < <= > >= • AND • OR • = (assignment) performed last Conditional statements (both simple relational expressions and compound conditional expressions) are evaluated as the program executes. The selection structure and the repletion structure are the two control structures that use condition statements to control the execution of the sets of instructions in the program.
Introduction to Condition StatementsMore about operator precedence You have completed the introduction to condition statements lessons MAIN MENU