Most programs do not work by executing a simple sequential set of statements. The code is constructed so that decisions and different paths through the program can be taken based on changes in variable values.
To make this possible all programming language have a set of control structures which allow this to happen.
In this episode we are going to look at how we can create loops and branches in our Python code. Specifically we will look at three control structures, namely:
- If..Else..
- While…
- For …
The print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statement and variants
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
The simple
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statement allows the program to branch based on the evaluation of an expressionThe basic format of the
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statement is:if expression :
statement 1
statement 2
...
statement n
statement always executed
If the expression evaluates to
Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
2 then the statements 1 to n will be executed followed by Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
3 . If the expression is Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
4, only Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
3 is executed. Python knows which lines of code are related to the print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statement by the indentation, no extra syntax is necessary.Below are some examples:
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
In the examples above there are three things to notice:
- The colon
7 at the end of theExample 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
9 line. Missing this out is a common error.print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- The indentation of the print statement. If you remembered the
7 on the line before, Jupyter [or any other Python IDE] will automatically do the indentation for you. All of the statements indented at this level are considered to be part of theExample 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
9 statement. This is a feature fairly unique to Python, that it cares about the indentation. If there is too much, or too little indentation, you will get an error.print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- The
9 statement is ended by removing the indent. There is no explicit end to theprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statement as there is in many other programming languagesprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
In the last example, notice that in Python the operator used to check equality is
print["\nExample 2a\n"]
a= 3
b= 4
print["a is", a, "b is",b]
if a > b :
print[a, "is bigger than ", b]
if a b :
print[a, "is bigger than ", b]
if a threshold :
print[value, "is above ", threshold, " threshold"]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 statements to decide which is larger we can use the print["\nExample 2a\n"]
a= 3
b= 4
print["a is", a, "b is",b]
if a > b :
print[a, "is bigger than ", b]
if a threshold :
print["above threshold"]
else :
print["below threshold"]
value = 4 and threshold = 5
below threshold
Exercise
Repeat above with different operators ‘ threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"] 9 statement is the
print["\nExample 2a\n"] a= 3 b= 4 print["a is", a, "b is",b] if a > b : print[a, "is bigger than ", b] if a b : print[a, " is greater than ", b] elif a == b : print[a, " equals ", b] else : print[a, " is less than ", b]
a = 5 and b = 4 5 is greater than 4
The overall structure is similar to the
3 loopprint["\nExample 2a\n"] a= 3 b= 4 print["a is", a, "b is",b] if a > b : print[a, "is bigger than ", b] if a b : print[a, "is bigger than ", b] if a b : print[a, "is bigger than ", b] if a b : print[a, "is bigger than ", b] if a b : print[a, "is bigger than ", b] if a threshold : print["above threshold"] else : print["below threshold"]
The while loop is used to repeatedly execute lines of code until some condition becomes False.
For the loop to terminate, there has to be something in the code which will potentially change the condition.
6 operators amongst others# while loop n = 10 cur_sum = 0 # sum of n numbers i = 1 while i threshold : print["above threshold"] else : print["below threshold"]
The statements after the while clause are only executed if the condition evaluates as True. Within the statements after the while clause there should be something which potentially will make the condition evaluate as 4 next time around. If not the loop will never end.Example 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
In this case the last statement in the loop changes the value of i which is part of the condition clause, so hopefully the loop will end. We called our variable 8 and not# If ... Else ... value = 4 threshold = 5 print["value = ", value, "and threshold = ", threshold] if value > threshold : print["above threshold"] else : print["below threshold"]
9 because# If ... Else ... value = 4 threshold = 5 print["value = ", value, "and threshold = ", threshold] if value > threshold : print["above threshold"] else : print["below threshold"]
9 is a builtin function [try typing it in, notice the editor changes it to green]. If we define# If ... Else ... value = 4 threshold = 5 print["value = ", value, "and threshold = ", threshold] if value > threshold : print["above threshold"] else : print["below threshold"]
1 now we can’t use the functionvalue = 4 and threshold = 5 below threshold
9 in this Python session.# If ... Else ... value = 4 threshold = 5 print["value = ", value, "and threshold = ", threshold] if value > threshold : print["above threshold"] else : print["below threshold"]
Exercise - Things that can go wrong with while loops
In the examples below, without running them try to decide why we will not get the required answer. Run each, one at a time, and then correct them. Remember that when the input next to a notebook cell is [*] your Python interpreter is still working.
0print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
1print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
2print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
3print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Solution
- Because i is incremented before the sum, you are summing 1 to 11.
- The Boolean value is set to False the loop will never be executed.
- When i does equal 10 the expression is False and the loop does not execute so we have only summed 1 to 9
- Because you cannot guarantee the internal representation of Float, you should never try to compare them for equality. In this particular case the i never ‘equals’ n and so the loop never ends. - If you did try running this, you can stop it using Ctrl+c in a terminal or going to the kernel menu of a notebook and choosing interrupt.
The
3 loopvalue = 4 and threshold = 5 below threshold
The for loop, like the while loop repeatedly executes a set of statements. The difference is that in the for loop we know in at the outset how often the statements in the loop will be executed. We don’t have to rely on a variable being changed within the looping statements.
The basic format of the
3 statement isvalue = 4 and threshold = 5 below threshold
4print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
The key part of this is the
5. The phrase used in the documentation is that it must be ‘iterable’. That means, you can count through the sequence, starting at the beginning and stopping at the end.value = 4 and threshold = 5 below threshold
There are many examples of things which are iterable some of which we have already come across.
- Lists are iterable - they don’t have to contain numbers, you iterate over the elements in the list.
- The
6 functionvalue = 4 and threshold = 5 below threshold
- The characters in a string
5print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
6print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Exercise
Suppose that we have a string containing a set of 4 different values separated by
7 like this:value = 4 and threshold = 5 below threshold
7print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Research the
8 method and then rewrite example 8 so that it prints the 4 components ofvalue = 4 and threshold = 5 below threshold
9value = 4 and threshold = 5 below threshold
Solution
8print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is above ", threshold, " threshold"] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
The format of
0 is very much like that of a record in a csv file. In later episodes we will see how we can extract these values and assign them to variables for further processing rather than printing them out.# If ... Elif ... Else ... EndIf a = 5 b = 4 print["a = ", a, "and b = ", b] if a > b : print[a, " is greater than ", b] elif a == b : print[a, " equals ", b] else : print[a, " is less than ", b]
Which control structure can be used to execute only if a condition is false in Perl?
While you could certainly use two if statements, one the negation of the other, Perl provides a more elegant solution. After the block, if can take an optional second condition, called else, to be executed only if the truth condition is false.Which control structure used whether a condition is true or false?
The selection structure tests a condition, then executes one sequence of statements instead of another, depending on whether the condition is true or false.Which is the loop that is executed when the condition is false?
The while[] loop If the condition is false the body of the loop never executes at all.Which control structure is used to check if a condition has been met or not?
The basic Control Structures in programming languages are: Conditionals [or Selection]: which are used to execute one or more statements if a condition is met. Loops [or Iteration]: which purpose is to repeat a statement a certain number of times or while a condition is fulfilled.