Control_Statements_PPT_ch03.ppt

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Fundamentals of Python: First Programs Chapter 3: Control Statements Objectives Use selection statements to make choices in a program Construct appropriate conditions for condition- controlled loops and selection statements Use logical operators to co...

Fundamentals of Python: First Programs Chapter 3: Control Statements Objectives Use selection statements to make choices in a program Construct appropriate conditions for condition- controlled loops and selection statements Use logical operators to construct compound Boolean expressions Use a selection statement and a break statement to exit a loop that is not entry-controlled Fundamentals of Python: First Programs 2 Objectives After completing this chapter, you will be able to: Write a loop to repeat a sequence of actions a fixed number of times Write a loop to traverse the sequence of characters in a string Write a loop that counts down and a loop that counts up Write an entry-controlled loop that halts when a condition becomes false Fundamentals of Python: First Programs 3 Selection: if and if-else Statements Selection statements allow a computer to make choices – Based on a condition Fundamentals of Python: First Programs 4 The Boolean Type, Comparisons, and Boolean Expressions Boolean data type consists of two values: true and false (typically through standard True/False) Example: 4 != 4 evaluates to False Fundamentals of Python: First Programs 5 if-else Statements Also called a two-way selection statement Often used to check inputs for errors: Syntax: Fundamentals of Python: First Programs 6 if-else Statements (continued) must be a Boolean expression Better alternative: Fundamentals of Python: First Programs 7 One-Way Selection Statements Simplest form of selection is the if statement Fundamentals of Python: First Programs 8 Multi-way if Statements A program may be faced with testing conditions that entail more than two alternative courses of action Can be described in code by a multi-way selection statement Fundamentals of Python: First Programs 9 Multi-way if Statements (continued) Syntax: Fundamentals of Python: First Programs 10 Logical Operators and Compound Boolean Expressions Often a course of action must be taken if either of two conditions is true: Below are two approaches – Could we use the and logical operator instead? Fundamentals of Python: First Programs 11 Logical Operators and Compound Boolean Expressions (continued) Fundamentals of Python: First Programs 12 Logical Operators and Compound Boolean Expressions (continued) Next example verifies some of the claims made in the previous truth tables: The logical operators are evaluated after comparisons but before the assignment operator – not has higher precedence than and and or Fundamentals of Python: First Programs 13 Logical Operators and Compound Boolean Expressions (continued) Fundamentals of Python: First Programs 14 Short-Circuit Evaluation In (A and B), if A is false, then so is the expression, and there is no need to evaluate B In (A or B), if A is true, then so is the expression, and there is no need to evaluate B Short-circuit evaluation: Evaluation stops as soon as possible Fundamentals of Python: First Programs 15 Testing Selection Statements Tips: – Make sure that all of the possible branches or alternatives in a selection statement are exercised – After testing all of the actions, examine all of the conditions – Test conditions that contain compound Boolean expressions using data that produce all of the possible combinations of values of the operands Fundamentals of Python: First Programs 16 Summary Control statements determine order in which other statements are executed in program Definite iteration is process of executing set of statements fixed, predictable number of times – Example: use for loop for loop consists of header and set of statements called body – Can be used to implement a count-controlled loop Use range to generate sequence of numbers – Can traverse and visit the values in any sequence Fundamentals of Python: First Programs 17 Summary (continued) A format string and its operator % allow programmer to format data using field width and precision An off-by-one error occurs when loop does not perform intended number of iterations, there being one too many or one too few Boolean expressions evaluate to True or False – Constructed using logical operators: and, or, not – Python uses short-circuit evaluation in compound Boolean expressions Selection statements enable program to make choices Fundamentals of Python: First Programs 18 Summary (continued) if-else is a two-way selection statement Conditional iteration is the process of executing a set of statements while a condition is true – Use while loop (which is an entry-control loop) A break can be used to exit a loop from its body Any for loop can be converted to an equivalent while loop Infinite loop: Continuation condition never becomes false and no other exit points are provided random.randint returns a random number Fundamentals of Python: First Programs 19

Use Quizgecko on...
Browser
Browser