Show
if Statements The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is true. A control structure is a logical design that controls the order in which a set of statements execute. sequence structure (a set of statements that execute in the order in which they appear.) Although the sequence structure is heavily used in programming, it cannot handle every type of task. This is because some problems simply cannot be solved by performing a set of ordered steps, one after the other. Programs like this require a different type of control structure: one that can execute a set of statements only under certain circumstances. This can be accomplished with a decision structure. (Decision structures are also known as selection structures.) The action is conditionally executed because it is performed only when a certain condition is true. single alternative decision structure: provides only one alternative path of execution. In Python, we use the if statement to write a single alternative decision structure. General format: 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.
What type of loop structure repeats the code a specific number of times?A count-controlled loop repeats a specific number of times.
When a function is called by its name during the execution of a program then it is?When a function is called by its name during the execution of a program, then it is. executed.
|