Skip to content

Control instructions

Program control

Python supports instructions that can change the order in which the code executes (normally from top to bottom).

These expressions are:

  • conditional statements (if, elif, else) - allow you to jump over certain blocks of code in the event that the condition for their execution is not met. Thanks to this, for example, the program will not try to read text from a file if this file does not exist.
  • loops (for, while) - thanks to the loop solutions, we can repeat the same code any time, loops allow you to shorten the number of lines of the program and to simplify it (more in the next part of the course).

Conditional instructions

In the real world, we often have to make some choices.

If it rains then I will take an umbrella with me.

Various decisions can (and even must) be made while the program is running:

If the user has not provided a first name, ask him / her to fill in this field again.

If the player has zero lives, end the game and display "Game over".

If there is no WiFi connection, try connecting to the internet via a mobile operator.

In programming, the instruction that allows you to make such decisions is the if statement.

The if statement

This is the simplest form of control instructions. She checks the logical condition and if it is true then instructions are executed in her body (inside the block), if not - they are skipped. The construction looks as follows:

    if <condition>:
        <instructions_if_condition_is_satisfied>

The block flow diagram is shown below:

If

  • The value <condition> must be able to be translated into a Boolean value (True/False).
  • If <condition> is true (translates to True), Python will execute <instructions_if_condition_is_satisfied >.
  • An indentation is required!

Each programming language must have a way to isolate the block of code from the rest. This solution is necessary, because otherwise, it would not be easy to tell the interpreter which lines of code to execute for the if statement when the condition is met. Everything that is under the expression if and is indented, compared to the rest of the code, is considered to belong to this block. Non-indented lines are not treated as block codes, so they may not be executed if a certain condition is met.

Here's a simple example of the if statement in Python:

    x = 0
    y = 3

    if x> y: # This will be translated to False because 0 is not greater than 3
        print (f "{x} is greater than {y}") # Will not be displayed

    if x <y: # This will be translated to True because 3 is greater than 0
        print (f "{x} is less than {y}") # Will be displayed

Indentation

  • Python recognizable character.
  • In order to execute more than one instruction in the if block, all instructions must be indented in the code.
    if <condition>:
        <instruction_1>
        <instruction_2>
        <instruction_3>
        ...
        <instruction_n>
    <following_instructions_outside_if_block>

Indentation is used in Python to create blocks of code or compound statements.

    x = 0
    y = 3

    if x> y: # This will be translated to False because 0 is not greater than 3
        print (f "{x} is greater than {y}") # Will not be displayed

    if x <y: # This will be translated to True because 3 is greater than 0
        print (f "{x} is less than {y}") # Will be displayed

    print ("This line will always be displayed")

The else clause

In the if else statement, the code in theelse block is executed if and only if the logical condition declared in the if statement is not met. The scheme of this construction can be saved as follows:

    if <condition>:
        <instructions_if_condition_is_satisfied>
    else:
        <other_instructions>

The else clause is optional, it can appear only once in one conditional statement.

The block flow diagram is shown below:

Else

A simple example of using the if else statement:

    x = 0
    y = 3

    if x> y: # This will be translated to False because 0 is not greater than 3
        print (f "{x} is greater than {y}") # Will not be displayed
    else: # This will be translated to True because 3 is greater than 0
        print (f "{x} is less than {y}") # Will be displayed

    print ("This line will always be displayed again")

Elif clause

There is also a way to make one of many choices depending on which of the available conditions will be met first. Hence the if ... else if ... else statement with any number of else if blocks, which gives the option of branching the expression into many conditions. The scheme of this construction can be written as follows:

    if <condition>:
        <instructions>
    elif <other_condition>:
        <other_instructions>
    elif <even_other_condition>:
        <even_other_instructions>
    else:
        <totally_other_instructions>
  • Any number of elif clauses in the conditional statement can be implemented.
  • The elif clause is optional.
  • If no condition (neither for if nor for any of the expressions elif) is met, the instructions in the else block (if added) will be followed.

An example of using the full if control statement with the elif and else clauses:

    x = 0
    y = 3

    if x> y: # This will be translated to False because 0 is not greater than 3
        print (f "{x} is greater than {y}") # Will not be displayed
    elif x == 3: # This will be translated to False because 0 is not equal to 3
        print (f "{x} is equal {y}")
    else: # This will be translated to True because 3 is greater than 0
        print (f "{x} is less than {y}") # Will be displayed

    print ("This line will always be displayed again")