Science, Maths & Technology
Author:

# Conditionals and Compound Statements

Updated Thursday, 25th June 2015

Learn how to execute several actions at once by using compound statements.

This page was published over five years ago. Please be aware that due to the passage of time, the information provided on this page may be out of date or otherwise inaccurate, and any views or opinions expressed may no longer be relevant. Some technical elements such as audio-visual and interactive media may no longer work. For more detail, see our Archive and Deletion Policy

## Compound Statements

Copyright: Dreamstime In most of the programs we've used so far, we wanted the computer to execute only a single statement if the value of a particular condition was true (or false). However, we often want several statements to be executed as a result of a condition evaluating to true (or false). We mentioned in passing that it was possible to have more than one statement in either the if clause or the else clause of an if statement. In fact, the use of braces ( {} ) in JavaScript has the effect of 'wrapping' together several statements into what is called a compound statement, as you will see in the else part of the if statement in the next activity. Conceptually JavaScript treats any set of statements enclosed in braces as a single statement. In other words, where JavaScript syntax requires a single statement, you can always use a sequence of statements enclosed in braces instead.

The use of braces where only a single statement appears in an if or an else clause is not strictly necessary, but we recommend that you use them as it makes the code more readable and helps avoid errors when modifying a program – for example, if you wanted to add an additional statement to one of the clauses later on.

## Activity 1

Suppose the code of the now-familiar bank balance program is modified and the initial bank balance set to 500, so that the new code is as follows.

var bankBalance, withdrawalAmount;
bankBalance = 500; withdrawalAmount = window.prompt('How much do you want to withdraw?','');
withdrawalAmount = parseFloat(withdrawalAmount);
if (withdrawalAmount > bankBalance)
{
document.write('You cannot do that!' + '<BR>');
}
else
{

bankBalance = bankBalance – withdrawalAmount;
document.write('Your bank balance is now £'+ bankBalance + '<BR>');
}
document.write('Goodbye');

Question: What would be the output in response to the following inputs?

(a) 750
(b) 250
(c) 500

(a) You cannot do that!
Goodbye

The condition (withdrawalAmount > bankBalance) evaluates to true, so the if clause would be evaluated, followed by the final line.

(b) Your bank balance is now £250
Goodbye

The Boolean expression evaluates to false, so the compound statement in the else clause would be evaluated, by first calculating the new bank balance (£250) and then displaying it. Finally, the last line would be displayed.

(b) Your bank balance is now £0
Goodbye

As in part (b), the Boolean expression evaluates to false, so the statements in the else clause would be executed, evaluating and displaying the bank balance and then displaying Goodbye.

## Hints for Writing Programs With Selection Structures

• Start by thinking what variables you will need to hold the data being input, processed and output and give them meaningful identifiers.
• Then decide what the condition is that you want to test, i.e. the expression you want to be either true or false.
• What do you want to do if the expression evaluates to true? If you want to execute more than one statement, it is essential that you form a single compound statement using {} (but we advise you to include the braces anyway).
• Is there anything particular you want to do if the expression evaluates to false (do you need an else part)? Again, remember to include braces.
• How do you want to end the program?

## Activity 2

(a) Open the file in the text editor and write a program which asks the user to input the current year and the year of their birth. If they input a year of birth which is earlier than, or equal to, the current year, the program works out how old they will be on their birthday this year If, however, they enter a year of birth and a current year such that the year of birth is later than the current year, the program asks them to re-enter both the year of birth and the current year before working out how old they will be on their birthday.

(b) How many times should you run the program in order to test that it works, and with what sorts of value?

(c) What happens if the user enters a year of birth later than the current year twice?

### Solution

(a) Here is our solution. Note the comment at the start of the program to tell the reader what it does.

/* Activity_7.3.8. Program to work out your age on your birthday this year */

var yearOfBirth, thisYear;
yearOfBirth = parseFloat(yearOfBirth);
thisYear = window.prompt('Please enter the current year', '2008');
thisYear = parseFloat(thisYear);
if (yearOfBirth > thisYear)
{
document.write('You have made a mistake somewhere');
yearOfBirth = parseFloat(yearOfBirth);
thisYear = window.prompt('Please re-enter the current year', '2008');
thisYear = parseFloat(thisYear);
}
document.write('<BR>' + 'On your birthday this year, you will be'
+ (thisYear - yearOfBirth));

You might have included an additional variable, called something like age or ageThisYear to hold the result of the age calculation before displaying it.

(b) You should run the program at least twice; once when you enter your year of birth and current year correctly (so that the if clause is skipped), and once when you enter a year of birth which is later than the year entered as the current year, so that the if clause is executed.

(c) The program will display the age as a negative number. For example if you put in your year of birth as 2008 and the current year as 1962, the program will calculate that you are –46 years old!

You have now got to the stage of writing rather complex programs, which are becoming increasingly difficult to read and understand. For advice on how to format your code for readability, try our article on JavaScript Style and Layout.

In the article Introducing the while statement you will learn how to keep repeating a section of code over and over again until some conditions becomes false, but now there's some other things to cover around conditional statements in An Introduction to More Complex Conditionals, the next article we recommend you progress onto.