# EZ Math Movie, Tutorials, Decisions, If ... Else Statement

### Do one or the other.

We are going to look at a longer form of the if statement. This form looks much like our first form, but with an extra set of curly brackets that make a block of code which runs when the Boolean statement is false. Remember, with the former, simpler if statement nothing happened when the Boolean statement was false; the program simply continued on. Now we will do things one way if the Boolean statement is true, and we do them another way if the Boolean statement is false.  To work this way, we need one more keyword: else.

### If, else flow of control

Here is a diagram that shows the flow of a program section that we will call an if, else statement. Note that in this if, else statement when the Boolean statement evaluates to true, then a certain job happens, but if it evaluates to false, then a different certain job happens. After one or the other job completes, the program continues on.

### Examples

Here are a few examples of these type of decisions. A sample Boolean statement that might be used to model the situation is given for each.

• In a quiz game, if the player's response is correct, then congratulate, or else give a hint.
• Boolean statement: (reply == 1). Is it true or false? (Assume 1 was the correct response.)
• In a probability experiment, if a coin toss is heads, increment the count for heads, or else increment the count for tails.
• Boolean statement: (toss == 1). Is it true or false? (Assume 1 stands for heads.)
• At an amusement park, for a certain ride if a child is not tall enough, then they are turned away, or else they can enjoy the ride.
• Boolean statement: (height < 36). Is it true or false?

### If, else statement syntax

To do all this in EZ Math Movie we need both the keyword if and another keyword, else. Below is some code that checks if x is equal to 2. If it is, this prints the text 'It is true.' If x is not 2, this prints 'It is false.' Note the parentheses around the Boolean statement and both pairs of curly brackets with the word else between them. The first set of curly brackets form what is often called the 'true block' or the 'true clause'. The second set, after else, form the 'false block' or 'clause'.

### The if, else statement details

The upper part of our if, else statement (the part above else) looks exactly like the simpler if statement we have already covered. So, past the first close curly bracket, the one that closes the true block, we have:

• The keyword else.
• After else, an open curly bracket placed often on its own line.
• Next come one or more program lines that make up the false block.
• A closing curly bracket comes last, usually on its own line.
• No final semi-colon is used.

### Running the example program

This example is much like the previous one, except a false, or else block has been added to make an if, else statement.

Note that this program has a path if the Boolean statement is true (the true block), and it has a different path (the false block) if the Boolean statement is false.

### The example program

0ptions

Go on input

Bounds:
Sorry, apparently this browser does not support HTML 5 canvas tag graphics.
Printer

Edit: Runtime          View: Starter Suggestion

To runtime:

Storage:

Editor width: Normal Wide

### And....

Next tutorial: Nested if statements

Here is a link to a related article at EZ Programing Demos:

### Other sites you may find interesting

Like Math and Physics?

Zona Land Education offers explanations, diagrams, and animations on these topics.
And it is all presented in a clear and casual manner.

Want to learn about programing computers?

EZ Programing Demos has an animated, slow motion, interactive programing language demonstrator.
The site presents basic ideas at a beginner's level.

Custom Search

Name:
Domain:

Name
0

Name:
Name
0