# Python Programming/Control Flow

| Previous: Operators | Index | Next: Decision Control |
As with most imperative languages, there are three main categories of program control flow:
* loops
* branches
* function calls
Function calls are covered in the next section.
Generators and list comprehensions are advanced forms of program control flow, but they are not covered here.

### Overview

Control flow in Python at a glance:

### Loops

In Python, there are two kinds of loops, 'for' loops and 'while' loops.

#### For loops

A for loop iterates over elements of a sequence (tuple or list). A variable is created to represent the object in the sequence. For example,
This will output
The for loop loops over each of the elements of a list or iterator, assigning the current element to the variable name given. In the example above, each of the elements in x is assigned to i.
A built-in function called range exists to make creating sequential lists such as the one above easier. The loop above is equivalent to:
Similar to the slicing operation, in the range function, the first argument is the starting integer (we can just pass one argument to the range, which will be interpreted as the second argument, and then the default value: 0 is used for the first argument), and the second argument is the ending integer but excluded from the list.
The next example uses a negative step (the third argument for the built-in range function, which is similar to the slicing operation):
This will output
The negative step can be -2:
This will output
For loops can have names for each element of a tuple, if it loops over a sequence of tuples:
This will output
Links:
* 4.2. for Statements, The Python Tutorial, docs.python.org
* 4.3. The range Function, The Python Tutorial, docs.python.org

#### While loops

A while loop repeats a sequence of statements until the condition becomes false. For example:
Will output:
Python's while loops can also have an 'else' clause, which is a block of statements that is executed (once) when the while condition evaluates to false. The break statement (see the next section) inside the while loop will not direct the program flow to the else clause. For example:
This will output:
Unlike some languages, there is no post-condition loop.
When the while condition never evaluates to false, i.e., is always true, then we have an infinite loop. For example,
This results in an infinite loop, which prints 1,2,3,4,... . To stop an infinite loop, we need to use the break statement.
Links:
* 3.2. First Steps Towards Programming, The Python Tutorial, docs.python.org

#### Breaking and continuing

Python includes statements to exit a loop (either a for loop or a while loop) prematurely. To exit a loop, use the break statement:
This will output
The statement to begin the next iteration of the loop without waiting for the end of the current loop is 'continue'.
This will not produce any output.

#### Else clause of loops

The else clause of loops will be executed if no break statements are met in the loop.
Another example of a while loop using the break statement and the else statement:
This will output:

#### White Space

Python determines where a loop repeats itself by the indentation in the whitespace. Everything that is indented is part of the loop, the next entry that is not indented is not. For example, the code below prints "1 1 2 1 1 2"
On the other hand, the code below prints "1 2 1 2 1 2 1 2"

### Branches

There is basically only one kind of branch in Python, the 'if' statement. The simplest form of the if statement simple executes a block of code only if a given predicate is true, and skips over it if the predicate is false
For instance,
You can also add "elif" (short for "else if") branches onto the if statement. If the predicate on the first “if” is false, it will test the predicate on the first elif, and run that branch if it’s true. If the first elif is false, it tries the second one, and so on. Note, however, that it will stop checking branches as soon as it finds a true predicate, and skip the rest of the if statement. You can also end your if statements with an "else" branch. If none of the other branches are executed, then python will run this branch.
Links:
* 4.1. if Statements, The Python Tutorial, docs.python.org

### Conclusion

Any of these loops, branches, and function calls can be nested in any way desired. A loop can loop over a loop, a branch can branch again, and a function can call other functions, or even call itself.

## Exercises

* Print the numbers from 0 to 1000 (including both 0 and 1000).
* Print the numbers from 0 to 1000 that are multiples of 5.
* Print the numbers from 1 to 1000 that are multiples of 5.
* Use a nested for-loop to prints the 3x3 multiplication table below
* Print the 3x3 multiplication table below.

## External links

* 4. More Control Flow Tools, The Python Tutorial, docs.python.org
| Previous: Operators | Index | Next: Decision Control |