13.3 Nesting If-Statements and Loops

We can combine any number of if-statements and loops in any order that we like. Consider the following program, which prints out appropriately timed messages for a rocket lift-off:

For each iteration of the for-loop, the countdown variable refers to a different value (starts at 10 and counts down to 1). No matter what its value, countdown is printed to the console with each iteration. Then, depending on the value that countdown has for a particular iteration, an additional message maybe printed as well.

In this next example, we have two different loops under two different branches of a conditional statement:

Here, because we used an if/else structure, the program will execute only one of the two loops. If the count_up variable is True, the program will print Counting up and display the numbers from 1 to 10 in ascending order. If instead count_up is False, the upward-counting block won’t even be examined, and instead, the program will print Counting down! and display the numbers from 10 to 1 in descending order.