鶹Լ

Using NOT

Sometimes we just need to check if a condition is False. We can use the Boolean expression NOT to test for a false condition.

Consider this simple Python (3.x) program that prints out all numbers from 0 to 100:

stop = False
count = 0
while not(stop):
	print(count)
	count = count + 1
	if count > 100:
		stop = True

This program uses selection to determine whether to carry on printing out the value of ‘count’:

  • The program examines the condition of the Boolean expression while not(stop), in line 3.
  • If the Boolean value of ‘stop’ is False, then the condition is True.
  • The program loops, incrementing the value of count by 1 each time.
  • Once the value of ‘count’ is greater than 100, the value of ‘stop’ is changed to the Boolean value ‘Tܱ’. The condition of the Boolean expression while not(stop), in line 3, is now False and the program stops iterating as a result.

Using a NOT expression can sometimes be quite confusing, but it can sometimes also be quite useful.