Astronomy Hour of Code
Activity 8: Conditional StatementsInstructions:
Now we are going to use a boolean variable in our program to make a decision about what we want the program to do next. In programming, we call these kind of statements "conditional statements" and we use them to control the branch we take in our program. In Quorum we use an if statement to start a conditional, followed by a boolean condition to be evaluated. If we want to check the status of the dome in the observatory, we will use the following conditional statement for our next line of code.
if domeIsOpen
If the condition domeIsOpen is true, then whatever code we put on the next lines will be executed by the computer. For now, let's just output a message notifying the user that the dome of the observatory is open. We often indent the next lines to make our code easier to read so that we can quickly see that this line will only be executed if the condition is true.
output "Dome is open. Ready to take images."
Now if the dome is not open, we need to tell the computer what alternative instructions to execute. This statement is optional and you don't have to include it if you are not going to do anything if the condition is false. The statement is only one word, else, and we will include it without indenting it since it is not part of the true condition statements. In our case, we want to output a different message so we are going to use it.
else
For now, let's output a message to notify the user that the dome of the observatory is closed, and let's indent this line to make it easier to read.
output "Dome is closed. Telescope is not ready to take images."
Finally, to close the if statement, we need to tell the computer that the block is finished. we do this with the word end.
end
When you are ready, select the green Run Program button below the editor on the right (second in the tab order).
Exploration Challenge (Optional):
Change the value of the domeIsOpen variable using one of the two methods you learned in the last Exploration Challenge so that the else branch of your conditional statement is executed and the computer outputs: Dome is closed. Telescope is not ready to take images.
Next Tutorial
In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.