<< Chapter < Page Chapter >> Page >

The output from the code in Listing 1 is shown in Figure 1 .

Figure 1 . Output from the code in Listing 1.
1-1-1 1-1-21-1-3 In else clause on inner while loop1-2-1 1-2-21-2-3 In else clause on inner while loopIn else clause on for loop 2-1-12-1-2 2-1-3In else clause on inner while loop 2-2-12-2-2 2-2-3In else clause on inner while loop In else clause on for loopIn else clause on outer while loop Goodbye

You should already be familiar with most of the code in Listing 1 . Some of the statements have been rearranged relative to the odometer code in theearlier module and some of the limits in the conditions have been reduced. However, that was done simply to shorten and to improve the clarity of theprinted output and is of no technical consequence.

The only thing that is new in Listing 1 is the addition of an else clause to each of the loops. Note that the indentation level of the word else in each case is the same as the indentation level for the corresponding words while and for . For example, the last else clause in Listing 1 belongs to the outermost while loop that begins with the word while near the top of Listing 1 .

In this program, each of the else clauses simply prints a message indicating that it is being executed. Obviously, more substantive code could beplaced in the else clause in a more significant program.

The innermost while loop in this program is executed twice for each iteration of its enclosing for loop. Similarly, the for loop is executed twice for each iteration of its enclosing while loop. The outermost while loop is executed once, the for loop is executed twice, and the innermost while loop is executed four times.

The else clause for a loop is executed each time the loop terminates (with the exception of break as mentioned earlier) . As you can see in Figure 1 , the else clause on the innermost while loop was executed four times, the else clause on the for loop was executed two times, and the else clause on the outermost while loop was executed once.

Visualize loops with else clauses

I recommend that you create a visualization for the code in Listing 1 and step through the program one instruction at a time. As you do that, pay attention to the movements of the red and green arrows on theleft, the diagram on the right, and the printed material at the bottom. That should help you to better understand the behavior of loops with else clauses.

The continue statement

If you execute a continue statement inside a while loop or a for loop, that will cause the current iteration of the loop to terminate and the next iteration to begin.

(Be careful that the code that updates the condition in a while loop is not bypassed by a continue statement. If you do, you will end up with aninfinite loop.)

The use of a continue statement is illustrated in Listing 2 , which shows an if statement (with a continue statement in the body of the if statement) inserted into the innermost while loop of the program from Listing 1 .

Listing 2 . Nested loops with a continue statement.
# Illustrates nested loops with else and continue # --------------------------------------------------------------------------leftDigit = 0 rightDigit = 0#Begin outer while loop while leftDigit<2: leftDigit += 1#Begin for loop for middleDigit in range(1,3):rightDigit = 0 #Begin innermost while loopwhile rightDigit<3: rightDigit += 1if rightDigit == 2: print("continue")continue print(str(leftDigit) + "-" + str(middleDigit) + "-" + str(rightDigit))else: print("In else clause on inner while loop")#end inner while loop with else clause else:print("In else clause on for loop") #end for loop with else clauseelse: print("In else clause on outer while loop")#end outer while loop with else clause print("Goodbye")

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask