A condition (or expression) which returns a Boolean value is called a Boolean expression. Before you can do any conditional (if) statements, you first need to learn how to use boolean expressions.
In the last section, we looked at some arithmetic operators, for example, +, – and *, and also the operation of concatenation, represented by +, on strings. All these operators act on two values of the same type and return a third value of the same type: adding two numbers will give a third number; concatenating two strings will give a third string.
firstValue = window.prompt('Please enter your first value','');
firstValue = parseFloat(firstValue);
secondValue = window.prompt('Please enter your second value','');
secondValue = parseFloat(secondValue);
firstBool = (firstValue < secondValue);
secondBool = (firstValue <= secondValue);
document.write('It is ' + firstBool + ' that '+ firstValue + ' < '
document.write('<BR>' + 'It is ' + secondBool + ' that '+ firstValue
+ ' <= ' + secondValue);
Note that when you want to declare several variables, you can put them all on the same line consisting of the reserved word var followed by the names of each of the variables, separated by commas. However, we recommend that you put variables of different kinds on different lines, as we have done above.
(a) 3 for the first value and 5 for the second.
(b) 117 for the first value and 32 for the second.
(c) 2 for both values.
(d) What do you think is meant by the symbols < and <=?
(a) The output should be:
It is true that 3 < 5
It is true that 3 <= 5
(b) The output should be:
It is false that 117 < 32
It is false that 117 <= 32
(c) The output should be
It is false that 2 < 2
It is true that 2 <= 2
(d) The Boolean expression (a < b) evaluates to true if the number a is less than the number b; otherwise, it evaluates to false. The Boolean expression (a <= b) evaluates to true if the number a is less than the number b or the number a equals the number b; otherwise, the value of (a <= b) evaluates to false.
|Operator symbol||Informal Description||Discussion||Examples|
|less than||(a < b) is true if the value of a is less than the value of b, othewise it is false||
(3 < 5)
(7 < 2)
(3 < 3)
|less than or equal to||(a <= b) is true if the value of a is less than the value of b, or is equal to the value of b, otherwise it is false||
(2 <= 5)
(2 <= 1)
(2 <= 2)
|greater than||(a > b) is true if the value of a is greater than the value of b, otherwise it is false||
(2 > 3)
(2 > 1)
(2 > 2)
|greater than or equal to||(a >= b) is true if the value of a is greater than the value of b, or is equal to the value of b, otherwise it is false||
(3 >= 2)
(3 >= 5)
(3 >= 3)
|equal to||(a == b) is true if the value of a is equal to the value of b, otherwise it is false||
(2 == 3)
(2 == 2)
|!=||not equal to||(a != b) is true if the value of a is not equal to the value of b, otherwise it is false||
(3 != 2)
(2 != 3)
(2 != 2)
Note the use of a 'double equals' (==) symbol for the equality operator. This distinguishes it from the assignment operator =.
If you are unfamiliar with the 'greater than' or 'less than' symbols, it may help you to remember which way round they go if you bear in mind that the larger value goes at the open end of the <, the smaller value at the pinched end, i.e. (smaller < larger) is true.
Remember that if (c < d) is true, then (d > c) is true, and if (c <= d) is true, then (d >= c) is true.
Question: Which of the comparison operators are binary operators?
They are all binary operators, as all of them operate on two values.
(a) (8 <= 2)
(b) ((16 != (4*4))
(c) (16 != 8)
(d) (13 > 5)
(e) (16 == (4*4))
Comparisons on strings consisting of all lower-case or all upper-case letters work by comparing the first letter in each string, then, if they are the same, the second letter in each string, and so on. This results in the same order as you would find in a dictionary. For example, ('aardvark' < 'cat') and ('cat' < 'cave') are true, but ('cat' < 'camel') is false, because the first two letters match, but the third letter of 'cat' comes after the third letter of 'camel'.
For numeric strings a string beginning with '1' is considered to be ‘less than’ a string beginning with '2', which is ‘less than’ a string beginning with '3', and so on.
For this activity, you'll need to download booleans_activity_2.html.
(a) Open the file in your text editor and modify the code so that the program will compare strings rather than numbers. Save your changes, and run the program in your browser to compare some strings of your choice.
(b) What happens now when you input 117 as your first value and 32 as your second?
(c) Can you explain the result you got in part (b)?
(a) In order to compare strings, you must delete the two statements involving parseFloat().
(b) When you input 117 as your first value and 32 as your second, you get the following output.
It is true that 117 < 32
It is true that 117 <= 32
(c) The result in part (b) is because the values are being treated as the strings '172' and '32', rather than as the corresponding numbers, and, as discussed above, any string beginning with '1' comes before any string beginning with '3' in the ordering of numeric strings.
You can see a working solution to part (a) by downloading our example.
Now that you are able to discover whether something is true or false, you can now use this information to carry out an if statement. We cover this in Simple Conditional Statements, which it is recommended you read next.