Flow Control Part Two - Hsanokklis/2023-2024-Tech-journal GitHub Wiki

Elements of Flow Control

  • Start with a part called condition and is followed by a block of code called the clause

Conditions

  • always evaluate down to a Boolean value True/False
  • a flow control statement decides what to do based on whether its condition is True or False.
  • Almost every flow control statement uses a condition

Blocks of Code

  • Lines of python code can be grouped together in blocks

There are 3 rules for blocks

  • blocks begin when the indentation increases
  • blocks can contain other blocks
  • blocks end when indentation decreases to zero or to a containing block's indentation

image

There are 3 blocks in the code above

if statements

if this condition is true, execute the code in the clause

if statements consist of the following

  • the if keyword
  • a condition (that is, an expression that evaluates to True or False)
  • a colon
  • Starting on the next line, an indented block of code

image

image

else statements

an if statement can be followed by an else statement. If this condition is true, execute this code. Or else, execute that code.

an else statement consists of the following

  • the else keyword
  • a colon
  • starting on the next line, an indented block of code

image

image

elif statements

  • stands for else if
  • provides another condition that is checked only if any of the previous conditions were false.

elif statements always consist of the following

  • the elif keyword
  • a condition (that is, an expression that evaluates to True or False)
  • a colon
  • starting on the next line, an indented block of code

image

image

image

image