Flow Control in Python

Flow Control in Python

Introduction

Usually, in the execution of a program, each individual line of code is run, from the beginning of the program to the end.

However, in some cases, some lines of code are only executed if a certain condition is met, else it runs the next line of code.

In this blog post, I will be using Python programming language to explain the concept of flow control.

What is Flow Control?

Flow control is the order in which each line or block of code is executed. Just as in a flowchart that represents a workflow, flow control works in a similar way.

For example:

1200px-LampFlowchart.svg.png

In the above flowchart, the case study is a lamp that is not working. The first step is to check if it is plugged in. If it is not plugged in, you plug it in for it to work.

However, if it is plugged in but not working, check if the bulb is burned out. If the bulb is burned out, change the bulb, else, take the lamp to be fixed by a professional. Flow control works the same way.

Elements of Flow Control

  • Conditions - Conditions are requirements that have to be met for something to be done. In programming, they are expressions that could either be True or False. Flow control statements are based on conditions and decide what to do next whether these conditions are True or False.

  • Blocks of Code – In python, lines of code can be arranged in blocks. Blocks are created by indentation and can contain other blocks of code. Blocks of code also end when indentation ends.

  • Flow Control Statements – These statements are the actual decisions your program will make during its execution.

Types of Flow Control Statements In Python

if Statements

In Python, if statements consist of:

  • The if keyword
  • A condition which evaluates to be True or False
  • A colon
  • An indented block of code starting on the next line

The logic behind an if statement is, ‘If the condition is true, then execute the next block of code’.

An example is:

greeting = ‘hello’
if greeting == ‘hello’:
    print('hi’)

In the above code, the variable ‘greeting’ was assigned the value ‘hello’. The program prints ‘hi’ after checking if the value of the variable is actually ‘hi’.

else Statement

The else clause is executed when the if statement’s condition evaluates to False. It consists of:

  • The else keyword
  • A colon
  • An indented block of code on the next line

The logic behind the else statement is, ‘if the condition is True execute the next block of code, else, execute this next block of code’.

The syntax is as follows:

name = ‘Tinaif name == ‘Joy’:
    print('Hi Tina’)
else:
    print(‘Hi Tina’)

elif Statement

elif is short for else if. It is used when a programmer wants many possible blocks of code to be executed. The elif statement comes after an if or another elif statement. It’s syntax consists of the following:

  • The elif keyword
  • A condition
  • A colon
  • An indented block of code on the next line

An example is:

name = ‘Flora’
if name == ‘Flo’:
    print(‘Hi Flo’)
elif name == ‘Joy’:
    print(‘Hi Joy’)

It is not guaranteed that at least one of the conditions must evaluate to True. All the conditions can evaluate to False. In that case, no block of code will be executed.

The else keyword can also come after the elif statement.

if name == ‘Alice’:
    print(‘Hi Alice’)
elif name == ‘Joy’:
    print(‘Hi Joy’)
else:
    print(‘hello stranger’)

while Loop Statement

The while statement makes a block of code execute over and over again as long as the while statement’s condition is True.

Its syntax consists of the following:

  • The while keyword
  • A condition
  • A colon
  • An indented block of code

An example is:

i = 0
while i < 5:
    print(‘hello world’)
    i = i + 1

In the above code, the variable i is assigned the value 0. In the condition following the while keyword, as long as the value of i is less than 5, 'hello world' is to be printed in the console. The value of i increases by 1 and the code is run again.

The code keeps running, printing ‘hello world’ until the value of i is greater than 5 and the condition evaluates to False.

When using the while statement, make sure to write it in a way that the condition ultimately evaluates to False, else the code will run infinitely.

Also, a shortcut called the break statement can be used to get the program to break out of a while loop.

while true:
    print(‘How are you?’)
reply = input()    
    if reply == ‘fine’:
        break
print(‘Thank you’)

In the above code, while the expression is True, the program prints. ‘How are you?’ and requires an input. If the input entered is equal to ‘fine’, the program breaks out of the while loop. If the input entered is not equal to ‘fine’, the program will keep printing out, ‘how are you?’

Another statement that can be used in a while loop is the continue statement. When the program reaches the continue statement, it goes back to the beginning of the while statement and reevaluates the condition.

Here’s an example:

while True:
    print(‘Please enter your name: ‘)
    name = input(‘’)
    if name != ‘Joy’:
        continue
    else:
        break
print(‘Hello Joy’)

In the above code, while True, the program asks to enter a name. If the name is not Joy, the program goes back to the beginning of the code to run it and still asks to enter a name. Once the name entered is Joy, the program breaks out of the while loop and prints out, ‘Hello Joy’

for Loop Statement

The for statement is used to execute a block of code a certain number of times. Its syntax consists of:

  • The for keyword
  • A variable name
  • The in keyword
  • A range of numbers or string
  • A colon
  • An indented block of code

An example is:

for i in ‘hello’:
    print(i)

In the above code, for each time the program runs, a character from the string is outputted. The output looks like this:

h
e
l
l
o

Another example is:

for x in range(5):
    print(x)

The output looks like this:

0
1
2
3
4

Python starts from 0 when counting.

Conclusion

Flow control statements help you write more intelligent programs as they help decide what part of the code should be run or not.

Thank you for reading through to the end of this article. I hope I have been able to simplify flow control for you.

Did you know these statements were called flow control statements?