Selection

The IF statement is used in programming to make selections, or decisions.

IF statements consist of:

  • the condition - what we are basing the decision on
  • what to do if the condition is true
  • what to do if the condition is not true (optional)

An example of what an IF statement could do is, IF the test score is over 50 then display a message saying “Well done”, otherwise display a message saying “You did not pass”.

In Python this would look like:

if test_score >= 50:
	print("Well done")
else:
	print("You did not pass")

In Python, the conditions can use logical operators to compare values:

  • > greater than
  • < less than
  • == equal to
  • >= greater than or equal to
  • <= less than or equal to
  • != not equal to