Conditional Expressions (If/Else)
This tutorial covers:
- What an ‘if then else' statement is
- How to write conditional expressions
- Example of how conditional expressions work
- What a nested conditional expression is
- How to write a nested conditional expression
- Example of a nested conditional expression
Before starting, you should be familiar with:
At the end, you should be able to:
- Understand what an ‘if-then-else' statement is
- Use an ‘if-then-else' statement
- Use a nested ‘if-then-else' statement
Sometimes you may want the resulting value of an expression to be dependent on a condition. In BoGL you can write a conditional expression, also known as an if/else statement or conditional statement. A conditional expression is composed of three sub-expressions.
To write a conditional expression, you must first write the keyword
if, followed by an expression that evaluates to a Bool. After writing this first expression, you write the keyword
then, followed by a second expression (which does not have to evaluate to a Bool). After writing this second expression, you then write the keyword
else, followed by a third expression (which also does not have to evaluate to a Bool). The line below shows that this will look like written out, with the word expression being a placeholder for where an actual BoGL expression would be.
if expression then expression else expression
The first expression (located after the keyword
if) in a conditional expression is the condition. This expression will determine which of the next two expressions to evaluate. The condition must evaluate to be a Bool(True or False) value.
The second expression (located after the keyword
then) is what is evaluated when the condition is True.
The third expression (located after the keyword
else) is what is evaluated when the condition is False.
Example: Coin Toss
Lets return to our example with Jack and Rosa, which we last visited in the functions tutorial.
The goal of the program below is to determine a first player for a game using a coin toss result.
The function determineFirstPlayer will return a Player value, and takes a TossResult value as an argument. Right now, no matter if the TossResult value is Heads or Tails, Rosa will always be returned by the determineFirstPlayer function. To change this function so that Jack or Rosa can be returned depending on the TossResult argument, we can use a conditional experssion.
The modified determineFirstPlayer function below will return Rosa or Jack depending on whether the value provided to it's TossResult parameter is Heads or Tails.
The condition of the conditional expression within this function is
coinSide == Heads. If this expression evaluates to True (which happens when the function's argument is Heads), then the entire conditional expression will evaluate to Rosa. If the condition evaluates to False (which happens when the function's argument is Tails), then the entire conditional expression will evaluate to Jack.
What happens when you type
determineFirstPlayer(Heads) into the interpreter below?
What happens when you type
determineFirstPlayer(Tails) into the interpreter below?
Nested Conditional Expressions
Conditional expressions can contain more conditional expressions. A conditional expression contained within another conditional expression is refered to as a nested conditional expression. These are useful for creating conditional expressions that can evaluate to more than two possible values.
Example: Dice Toss
Jack and Rosa decide to play a game that involves four more players: Husein, Amanda, Stanley, and Ursa. Since there are now six players, they decide to use a six-sided die result to determine a first player. Shown below is a program that takes a die roll result as input and outputs the name of the chosen first player.
Jack and Rosa decide they want to determine who goes first in their game based off of three coin toss results instead of one. If there are more heads than tails, then Rosa will be first player, otherwise (if there are more tails than heads) Jack will be first player. Using nested conditional expressions, modify the
determineFirstPlayer function in the editor below so that all three TossResult parameters are used to determine a first player.
Click to see a solution (but try yourself first!)