Compound Boolean Expressions
Conditions can be more complex than those you have seen so far.
- Boolean conditions can be negated using the Boolean operator NOT.
- NOT is a unary Boolean operator with the following effect: if a is true, NOT a is false; if a is false, NOT a is true.
- Compound conditions can be expressed by combining simple conditions using the Boolean operators AND and OR.
- AND is a binary Boolean operator with the following effect: if both a and b are true, a AND b is true; otherwise (if either a or b is false), a AND b is false.
- OR is a binary Boolean operator with the following effect: if either a or b is true, or both are true, a OR b is true; otherwise (if neither a nor b is true) a OR b is false.
- Different sets of actions may need to depend on different conditions.
(a) Either (x > 3) OR (x <= 1).
(b) Both (x > 3) AND (x <= 5)).
(c) It is not true that (x > 10).
(a) ((x > 3) || ( x <= 1))
(b) ((x > 3) && (X <= 5))
(c) !(x > 10) (An acceptable alternative is (x <= 10).)
(a) ((x == 21) && (x > 22))
(b) !(x >= 21)
(c) ((x > 15) || (x < 12))
(a) false, since the second condition evaluates to false.
(b) false, since (x >= 21) evaluates to true.
(c) true, since the first condition evaluates to true.
(a) How would you modify line (3) of the code below:
(1) var response;
(2) response = window.prompt('Are you having a good day today?', '');
(3) if (response == 'Yes')
(5) document.write('Glad to hear it');
(9) document.write('Maybe tomorrow will be better');
(11) document.write('<BR>' + 'Thank you for replying');
so that an input of either Yes or Y (but nothing else) will produce an output of:
Glad to hear it
Thank you for replying.
(3) if (response == 'Yes')
(3) if ((response == 'Yes' ) || (response == 'Y'))
(b) On the assumption that everybody aged between 5 and 16 (inclusive) must be in full-time education, how would you fill in the Boolean condition in the following code?
age = window.prompt('Please enter your age','');
if (add the missing condition)
document.write('You must be in full-time education');
The missing condition would be:
if ((age >= 5) && (age <= 16))
We shall now consider how we might deal with the situation where we want the computer to execute one statement if a certain condition is true; a different statement if some other condition is true; a different statement again if a third condition is true, and so on. One way of doing this is by means of nested if statements, as described in the activity below.
Suppose we want to write a program that prompts the user to enter the current temperature in degrees Celsius and then displays the relevant description of the temperature for that input, as indicated in the figure below.
Here is our code, which is from complex_activity_2.html.
temperature = window.prompt('Please enter today\'s current
temperature in degrees Celsius','');
temperature = parseFloat(temperature);
if (temperature <= 0)
document.write('It is freezing');
if (temperature < 15)
document.write('It is cold');
if (temperature < 25)
document.write('It is a nice day');
document.write('It is hot!');
Notice also how we’ve indented our code to show which else goes with which if.
Question: Without running the program, would you expect the output from the above program to be for the following inputs?
(a) It is hot!
(b) It is cold
(c) It is freezing
(d) It is a nice day
The way in which nested conditionals work is shown in the figure below.
In a certain university, a student taking an exam may obtain pass, fail, or honours. The mark required for a pass is 40 or over, and the mark required for honours is 60 or over.
Sorry, you've failed or
Congratulations, you've passed or
Many congratulations, you have honours, as appropriate.
Hint: See the previous activity for a reminder about how you can display the apostrophe in you've.
(b) How many times should you run the program in order to test it?
(a) Here is our code. (As usual, your code may be different but correct.)
mark = window.prompt('Please enter your mark','');
mark = parseFloat(mark);
if (mark < 40)
document.write('Sorry, you\'ve failed');
if (mark < 60)
document.write('Congratulations, you\'ve passed');
document.write('Many congratulations, you have honours');
You can download our solution to this task if you wish.
(b) You should run the program to test all the possible routes through the program. There are three possible routes, one when the input is less than 40, one when it is 40 or over but less than 60, and one when it is greater than or equal to 60. To be really thorough, you should also test the boundary values in case you've made a mistake in your choice of comparison operators. In this case the boundary values are 39, 40, 41, 59, 60 and 61.