Skip to main content

Flow Control

Controlling your application with these techniques enable full control of how your application behaves.  They flex the power of conditional operators.

 

if.. elif.. else..
this = true
that = false

if this == true:
  print('this is true')
elif that == true:
  print('that is true')
else:
  print('neither this or that is true, how did that happen?')

 

for
# The range() function creates a list of numbers based on the parameter given
# The 'for' iterates through that list and stores the current item in x

# the first simple example will print the numbers 0-10
# the second one shows how to terminate a loop early based on a condition
# the last example does the same as the first two but using a pre-defined list

for x in range(10):
  print(x)
  
for x in range(100):
  if x > 10:
    break
  else:
    print(x)
    
listOfThings = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for x in listOfThings:
  print(x)

 

while
# This example loops while a condition evaluates to true

x = 0

while x <= 10:
  print(x)
  x++
match
# This is similar to a long list of if else statements but is easier to read.
# It branches code execution based the evaluation of a variable

x = 2

match x:
  case 1:
    print('x = 1')
  case 2:
    print('x = 2')
  case 3:
    print('x = 3')