成人快手

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 鈥榗ount鈥:

  • The program examines the condition of the Boolean expression while not(stop), in line 3.
  • If the Boolean value of 鈥榮top鈥 is False, then the condition is True.
  • The program loops, incrementing the value of count by 1 each time.
  • Once the value of 鈥榗ount鈥 is greater than 100, the value of 鈥榮top鈥 is changed to the Boolean value 鈥楾谤耻别鈥. 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.