Which control structure can be used to execute only if a condition is false?
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. Show
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:
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 variantsThe simple 9 statement allows the program to branch based on the evaluation of an expressionThe basic format of the 9 statement is:
If the expression evaluates to 2 then the statements 1 to n will be executed followed by 3 . If the expression is 4, only 3 is executed. Python knows which lines of code are related to the 9 statement by the indentation, no extra syntax is necessary.Below are some examples:
In the examples above there are three things to notice:
In the last example, notice that in Python the operator used to check equality is 3.
Instead of using two separate 9 statements to decide which is larger we can use the 5 construct
A further extension of the 9 statement is the 7 version.The example below allows you to be more specific about the comparison of a and b.
The overall structure is similar to the 8 statement. There are three additional things to notice:
The # If ... Else ... value = 4 threshold = 5 print("value = ", value, "and threshold = ", threshold) if value > threshold : print("above threshold") else : print("below threshold") 3 loopThe 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.
Points to note:
The value = 4 and threshold = 5 below threshold 3 loopThe 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 is 4The 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.There are many examples of things which are iterable some of which we have already come across.
5 6
|