1 of 20

Basics of Python

Iteration

2 of 20

Lecture #5

Iteration

3 of 20

Iteration

  • Iteration repeats the execution of a sequence of code.
  • Iteration is useful for solving many programming problems.
  • Iteration and conditional execution form the basis for algorithm construction.

4 of 20

The while Statement

5 of 20

The while Statement

6 of 20

The while Statement

  • The execution of while, checks the condition before executing the while block and then checks the condition again after executing the while block.
  • As long as the condition remains truth, the program repeatedly executes the code in the while block.
  • If the condition initially is false, the program will not execute the code within the while block.
  • If the condition initially is true, the program executes the block repeatedly until the condition becomes false, at which point the loop terminates.

7 of 20

The while Statement

8 of 20

The while Statement

9 of 20

Definite Loops vs. Indefinite Loops

10 of 20

The for Statement

  • The while loop is ideal for indefinite loops.
  • Python provides a more convenient way to express a definite loop.
  • The for statement iterates over a sequence of values.
  • One way to express a sequence is via a tuple, as shown here:

11 of 20

The for Statement

The general form of the range expression is:

12 of 20

The for Statement

  • Initially emphasized the for loop’s ability to iterate over integer sequences because this is a useful and common task in software construction.
  • The for loop, however, can iterate over any iterable object.
  • A tuple is an iterable object, and a range object is an iterable object. A string also is an iterable object.
  • We can use a for loop to iterate over the characters that comprise a string.

13 of 20

Nested Loops

  • Just like with if statements, while and for blocks can contain arbitrary Python statements, including other loops.
  • A loop can therefore be nested within another loop.

14 of 20

Abnormal Loop Termination

  • Normally, a while statement executes until its condition becomes false.
  • A running program checks this condition first to determine if it should execute the statements in the loop’s body.
  • It then re-checks this condition only after executing all the statements in the loop’s body.
  • Ordinarily a while loop will not immediately exit its body if its condition becomes false before completing all the statements in its body.
  • The while statement is designed this way because usually the programmer intends to execute all the statements within the body as an indivisible unit.
  • Sometimes, however, it is desirable to immediately exit the body or recheck the condition from the middle of the loop instead.

15 of 20

Abnormal Loop Termination

16 of 20

The break statement

  • Python provides the break statement to implement middle-exiting loop control logic.
  • The break statement causes the program’s execution to immediately exit from the body of the loop.

17 of 20

The continue Statement

  • The continue statement is similar to the break statement, except the continue statement does not necessarily exit the loop.
  • The continue statement skips the rest of the body of the loop and immediately checks the loop’s condition.
  • If the loop’s condition remains true, the loop’s execution resumes at the top of the loop.

18 of 20

Infinite Loops

  • An infinite loop is a loop that executes its block of statements repeatedly until the user forces the program to quit.
  • Once the program flow enters the loop’s body it cannot escape. Infinite loops sometimes are by design.
  • For example, a long-running server application like a Web server may need to continuously check for incoming connections.
  • The Web server can perform this checking within a loop that runs indefinitely.

19 of 20

Q&A

20 of 20

Q&A