Notes
This preview shows page 1 - 2 out of 2 pages. Chapter 4Control structureis a logical design that controls the order in which a set ofstatements executesDecision structure(aka selection structure) allows a program to performactions only under certain conditions.ouse to handle any part of a program that needs to test a condition andconditionally execute an action depending on the outcome of thecondition.Single alternative decision structure: only provides one alternative path ofexecutionoif condition is true(or false) we do something, otherwise we exit thestructureoif-then statementIfconditionThenstatementstatementstatementetc.End Ifocondition must be a Boolean expressionotested by relational operator’Case structure (multiple alternative decision structure): allows you to testvalue of variable or an expression and use that value to determine whichstatement or set of statements to executeoin pseudocode: useSelect CasestatementoSelecttestExpression End of preview. Want to read all 2 pages? Upload your study docs or become a Course Hero member to access this document Tags relational operator, Switch statement, or operator Presentation on theme: "The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute."— Presentation transcript:1
2 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 3 The if Statement (cont’d.) 4 The if Statement (cont’d.)
5 The if Statement (cont’d.) 6 Boolean Expressions and Relational Operators 7
Boolean Expressions and Relational Operators (cont’d.)
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 Example # This program gets three test scores and displays their average. It congratulates the user if the average is high. high_score = 95 # Get the three test scores. test1 = int(input('Enter the score for test 1: ')) test2 = int(input('Enter the score for test 2: ')) test3 = int(input('Enter the score for test 3: ')) # Calculate the average test score.
average = (test1 + test2 + test3) / 3 # Print the average. print('The average score is', average) # If the average is a high score, congratulate the user. if average >= high_score: print('Congratulations!') print('That is a great average!')
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 # Variables for base
hours and the overtime multiplier. What is the structure that causes a statement or a set of statements to execute repeatedly?A repetition structure causes a statement or set of statements to execute repeatedly. Repetition structures are used to perform the same task over and over. A condition-controlled loop uses a Boolean (true/false) condition to control the number of times that it repeats.
What structure can execute a set of statements only under certain circumstances?Decision structure executes a set of statements under certain conditions.
Which of the following is a logical operator group of answer choices?There are three logical operators: and , or , and not .
Which of the following operators reverses the logic of its operand?The logical negation operator ( ! ) reverses the meaning of its operand.
|