Which structure is a logical design that controls the order in which a set of statements executes?

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.)
In flowchart, diamond represents true/false condition that must be tested Actions can be conditionally executed Performed only when a condition is true Single alternative decision structure: provides only one alternative path of execution If condition is not true, exit the structure

4 The if Statement (cont’d.)

5 The if Statement (cont’d.)
Python syntax: if condition: Statement First line known as the if clause Includes the keyword if followed by condition The condition can be true or false When the if statement executes, the condition is tested, and if it is true the block statements are executed. Otherwise, block statements are skipped

6 Boolean Expressions and Relational Operators
Boolean expression: expression tested by if statement to determine if it is true or false Example: a > b true if a is greater than b; false otherwise Relational operator: determines whether a specific relationship exists between two values Example: greater than (>)

7 Boolean Expressions and Relational Operators (cont’d.)
>= and <= operators test more than one relationship It is enough for one of the relationships to exist for the expression to be true == operator determines whether the two operands are equal to one another Do not confuse with assignment operator (=) != operator determines whether the two operands are not equal

8 Boolean Expressions and Relational Operators (cont’d.)

9 Boolean Expressions and Relational Operators (cont’d.)
Using a Boolean expression with the > relational operator

10 Boolean Expressions and Relational Operators (cont’d.)
Any relational operator can be used in a decision block Example: if balance == 0 Example: if payment != balance It is possible to have a block inside another block Example: if statement inside a function Statements in inner block must be indented with respect to the outer block

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.
base_hours = # Base hours per week ot_multiplier = # Overtime multiplier # Get the hours worked and the hourly pay rate. hours = float(input('Enter the number of hours worked: ')) pay_rate = float(input('Enter the hourly pay rate: ')) # Calculate and display the gross pay. if hours > base_hours: # First, get the number of overtime hours worked. overtime_hours = hours - base_hours # Calculate the amount of overtime pay. overtime_pay = overtime_hours * pay_rate * ot_multiplier # Calculate the gross pay. gross_pay = base_hours * pay_rate + overtime_pay else: # Calculate the gross pay without overtime. gross_pay = hours * pay_rate # Display the gross pay. print('The gross pay is $', gross_pay)

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.