TRUE or FALSE in programming
Introduction
Boolean algebra is often used in programming. You frequently have to test a value and if the result is TRUE then you run one block of code but if it's FALSE, you run a different block of code. This is called 'selection' in programming. We write some pseudo-code to represent using this selection construction using the IF statement.
IF <the result of a test is TRUE>
do the code
you find in this
block and then exit
ELSE
do the code
you find in this
block and then exit
ENDIF
Most programming languages have an IF keyword so that you can use the selection construction. The actual syntax (the way the construction is written and the keyowrds you must use) differs from language to language but the idea is the same for all languages. The tests use maths operators. These are typically >, >=, <, <=, == and !=.
How do you test numbers in programming?
If we had to test to see if a test result held in a variable called score is greater than the pass mark, we might write this:
IF (score > 50)
PRINT 'You have passed. Well done'
ELSE
PRINT 'You have failed and must have extra lessons after school'.
Score > 50 can have one of two outcomes. It can be a TRUE statement or a FALSE statement. Either your score is greater than 50 (TRUE) or it's not (FALSE).
You compare actual values using maths operators:
45 > 23 (TRUE) greater than
34 < 4 (FALSE) less than
12 >= 12 (TRUE) greater than or equal to
14 <= 20 (TRUE) less than or equal to
26 == 26 (TRUE) equal to
34 != 34 (FALSE) not equal to
You can compare values held in variables:
Score > passmark
age < legalAge
eggs >= omelette
height <= bar
speed == maximum
left != right
How do you test strings in programming?
All of the above are testing numbers but you can test words as well. Any mixture of letters, numbers and symbols in programming is known as a string. Just remember to put strings in quotes, to tell the computer you are testing strings. There is a big difference in programming between the number 5 and the symbol '5', for example, and this is often a cause of bugs in a program when you start out learning how to code. When comparing strings, your computer converts each letter into its ASCII code and compares those numbers! It's useful to have an ASCII table handy.
You could test the following strings:
"David" > "John" (False, because the ASCII code of D is less than the ASCII code of J.)
"Money" < "Monster" (TRUE, because the first letters are the same, as are the second and third (so they have the same ASCII codes), but the ASCII code of e is less than the ASCII code of s.)
"sad" < "Saddle" (FALSE, because s has an ASCII code 115 but the ASCII code of S is 83, and as 115 < 83 is FALSE, the answer is FALSE.)
"sad" < "saddle" (TRUE, because sad are the same, then you have no letter in the first word, which is nul, or ASCII code 0. d is ASCII code 100, and as 0 < 100, the answer is TRUE.)
Q1. For each of the following, state whether the answer is TRUE or FALSE.
a. 10 > 20
b. 20 < 40
c. 10 >=10
d. 5 <= 9
e. 34 == 31
f. 23 != 45
g. 17 >= 50
h. 45 != 45
Q2. For each of the following, state whether the answer is TRUE or FALSE and explain how you arrived at your answer.
a. "Jack" > "Jill"
b. "Kevin" < "kevin"
c. "Door" > "Doormouse"
d. "A work of art" < "All is not well".
e. "Blue and green" > "Blue and red"