Flow Control¶
There are two types of flow control statements supported by Python:
1. Conditionals¶
There are times in life when you need to make decisions, and these decisions depend on certain conditions. For example, suppose that you are in a class, then a decision that you might have to make would be:
\(\color{red} \text{if} \; \color{yellow} \text{you have a pen,} \; \color{red} \text{then} \; \color{white} \text{you can write on a piece of paper,} \; \color{red} \text{else} \; \color{white} \text{you borrow a pen}\)
Similarly, when writing a program, you might need to make decisions at some points in your code. Conditionals are decision making statements that can be used to chose which set of instructions to execute depending on given conditions.
1.1. The if else if
statement¶
An if else
statement is used whenever you need your program to make decisions. It executes one set of instructions if
a conditon is true or else
it executes another set of instructions.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 |
|
when writing if else statements, an indentation, usually 4 spaces, is required. Python uses these indentations to understand what part of the code is inside the if/else.
An if
statement does not need to be followed by an else
statement everytime
1 2 3 4 |
|
What if you need to check multiple conditions and do separate things for each case? this is when you use an if else if
statement!
1 2 3 4 5 6 7 8 |
|
What happens when multiple conditions are true?
In the above example, both the (b > a)
and (b == 20)
conditions are true. However, in an if else if
statement, only one branch of instructions is ever executed. Which condition takes proiority is decided by what order you write them in. So in this case, "b > 20"
will be printed to the screen because that is the first condition which is true.
Technically, whenever a condition becomes true and its branch of instructions are executed, all of the remaining conditions are skipped, and not even evaluated.
Now that you are armed with the power of if else if
, can you:
Write a program that would print the maximum of 3 given variables.
When you're ready, click the "Answer" to view the solution.
Now that you are armed with the power of if else if
, can you:
Write a program that would print the maximum of 3 given variables.
When you're ready, click the "Answer" to view the solution.
1 2 3 4 5 6 7 8 9 |
|
2. Loops¶
There are times in life when you need to repeatedly keep doing something under certain conditions. For example, suppose that you are playing a game and you are stuck on a boss fight where you keep dying, something that you are doing might be:
\(\color{red} \text{while} \; \color{yellow} \text{you have not defeated the boss,} \; \color{white} \text{try again}\)
If you wanted to write out the times two table, you might do:
\(\color{red} \text{for} \; \color{yellow} \text{every} \; \color{green} \text{number} \; \color{red} \text{between 1 and 10} \; \color{white} \text{write }2\times \color{green} \text{number}\)
Similarly, when writing a program, it might be needed to repeat certain parts of your code multiple times. Loops are statements that can be used to repeatedly execute a block of code given a condition is true.
Sometimes the need arises to repeatedly execute the same statement or a statement where only a few things are changing. Loops are statements that allow us to do exactly that! There are two types of loops suported by Python:
2.1. The while
loop¶
A while
statement repeatedly executes a block of code as long as (while) something is True
. This process of repeatedly executing the same block of code is known as iteration!
For example:
1 2 3 4 |
|
Now that you are armed with the power of while
, can you:
Write a program that would print the following pattern:
1, 2, 4, 7, 11, 16... up to 15 terms?
If you need help, but don't want to see the full solution immediately, click "Hint"
When you're ready, click "Answer" to view the solution.
Now that you are armed with the power of while
, can you:
Write a program that would print the following pattern:
1, 2, 4, 7, 11, 16... up to 15 terms?
If you need help, but don't want to see the full solution immediately, click "Hint"
When you're ready, click "Answer" to view the solution.
Hint: Notice that the pattern here is that each time, the increase of the terms is also going up by one. The 2nd term is the first term + 1, the 3rd term is the 2nd term + 2, and so on.
Now that you are armed with the power of while
, can you:
Write a program that would print the following pattern:
1, 2, 4, 7, 11, 16... up to 15 terms?
If you need help, but don't want to see the full solution immediately, click "Hint"
When you're ready, click "Answer" to view the solution.
Hint: Notice that the pattern here is that each time, the increase of the terms is also going up by one. The 2nd term is the first term + 1, the 3rd term is the 2nd term + 2, and so on.
1 2 3 4 5 6 |
|
2.2. The for
loop¶
A for
statement is specifically used to loop over a range of values, say 5
to 23
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
For loops can also be used to loop through all the elements of a list, tuple or a dict:
With lists:
1 2 3 4 |
|
With tuples:
1 2 3 4 |
|
With dicts:
1 2 3 4 |
|
2.3. The break
statement¶
break
forces a loop to terminate earlier, even if its condition is still True
:
1 2 3 4 5 6 |
|
This will just print:
1 2 3 4 5 |
|
Notice that the loop's condition is still True
at the end of this program
2.4. The continue
statement¶
continue
makes the loop immediately jump to its next cycle and skip any code after the continue
1 2 3 4 5 6 |
|
This outputs:
1 2 3 4 |
|
Notice that printing the value 2
was skipped, but the others were still printed
2.5. The while else
statement¶
This is a unique feature of python that is not found in other programming languages. You can add an else block at the end of a while loop as follows:
1 2 3 4 5 6 |
|
What does this mean? If the condition of the while loop evaluates to False
, then the code in the else
block is executed. This is just like an if else
statement! So the above code will output:
1 2 3 4 |
|
But wait, doesn't that mean that an else block will ALWAYS run at the end of a loop when the loop condition eventually becomes False
for it to terminate? Yep! Now you might be asking, how is it any different from:
1 2 3 4 5 6 |
|
And the answer is, for this specific piece of code, there is no difference, and it should be written without the else
The difference is when we terminate our loop by other methods, remember the break
statement?
1 2 3 4 5 6 7 8 |
|
This outputs:
1 2 3 4 5 |
|
2.6. The for else
statement¶
This works exactly like the while else
statement, where the else
block is only executed when the loop terminates without a break
1 2 3 4 5 6 |
|
This outputs:
1 2 3 4 5 6 |
|