What is control flow?


#1

Question

In the context of this exercise, what is control flow?

Answer

Control flow is the concept of how a sequence of steps in a program flows, or runs from start to finish. The flow of a program can be dependent on factors such as the results of conditional statements, which utilize if/else if/else statements.

For example, we might have a Python program that prints out some message depending on the input value of the user. It will first run the try block code, and if the user input is not valid (not a numerical value) and brings up an error, then it will run the except block instead.

try:
  age = int(raw_input("Enter your age:"))
  if age < 18:
    print("You must be 18 or over to enter the race")
  else:
    print("You can enter the race")
except:
  print("The inputted age was invalid")

#2

When an instruction has been executed, control is handed to the next instruction. Flow describes which branch is followed from a conditional.