Part 1E) Conditional Statements in Quorum
The link to the PDF containing the text file of commands for Part 1E is below:
Quorum Commands Section 1 Part E – PDF
Sometimes a decision has to be made by the computer about what to do next. The commands used to control this are called “conditional statements”. These types of commands, or statements, control the branch taken in the program. Let’s listen as Chris compares conditional statements to navigating with a mobility cane in this 4:38 video:
Now let’s learn to use conditional statements in Quorum.
One type of conditional in Quorum is an IF statement followed by a BOOLEAN.
- Recall, a boolean is a data type with only two values, for example, on or off, true or false, etc.
The program follows the appropriate branch depending on the value of the boolean.
- The boolean data type MUST be defined before the if statement.
EXAMPLE:
First define a boolean variable, then use the if statement.
boolean domeIsOpen = true
if domeIsOpen
If the boolean variable ‘domeIsOpen’ condition is true, then whatever code we put on the next lines will be executed by the computer.
For now, just output a message notifying the user that the dome of the observatory is open.
- Often, the lines following the if statement are indented. It makes the code easier to read.
The next lines in the script will only be executed if the condition is true.
output “Dome is Open. The Telescope is ready to take images.”
Now if the dome is not open, the computer needs to know what alternative instructions to execute.
This command is optional and it doesn’t have to be included if nothing will be done if the condition is false.
- The command is only one word, ‘else‘.
- It will not be indented since it is not part of the true condition statements.
- In this case, a different message will be stated as an output statement.
else
output “Dome is Closed. The Telescope is NOT ready to take images”
Finally, to close the ‘if’ statement, the computer needs to be told that the block is finished. This is done with the word end.
end
Type all the above code into an open Quorum Box.. Remember, copying and pasting might give errors that are not easy to see. Select the BUILD button first to see if there are any errors. When satisfied, click on RUN.
More On Conditionals
An if statement does not have to be followed by a boolean. It can be followed by any valid statement.
EXAMPLE:
- Define a number variable: in this case, ‘a’ is used.
- ask a user to input a numeric answer to a question, for example, “How old are you?”.
- This input must be “cast” into a number variable.
- Then make a conditional statement about a:
The example is below:
if a > 21
The computer will now follow a path of instructions depending on if the statement is true or not.
- Remember, the computer stores any input as a text variable, so you must ‘cast’ it into an integer or number variable.
In the example below, replace the inputs and outputs with your choice as you type into a Quorum Box:
number a = cast
(number, input(“How old are you”))
Add an if statement:
if a < 18
output “You are not an adult”
else
output “You are an adult”
end