Astronomy Hour of Code

Activity 7: Using a Boolean Variable

Instructions:

Another type of variable we need to understand is called a boolean variable. A boolean variable is like a switch and can only have one of two values: true or false. We create a boolean variable in the same way we have created other variables, we specify the type of the variable, the variable name and the initial value.

When astronomers want to control a telescope, they need to check if the dome of the observatory is open or closed before they can take a picture. Let's create a boolean variable called domeIsOpen to hold a value we can check to see if the dome is open at the current time. If we start by saying the dome of the observatory is open, we will give our variable a value of true and our first line of code should look like this:

boolean domeIsOpen = true

Now put a simple output statement to display the value of the variable in the console.

output domeIsOpen

When you are ready, select the green Run Program button below the editor on the right (second in the tab order).

Exploration Challenge (Optional):

Add two lines of code to switch the value of the domeIsOpen variable to false and then output the new value. To get the opposite of the current value of a boolean variable, you can also use the logical operator not if you prefer. Try to use not keyword in an output statement to get the opposite value of your variable.

Next Tutorial

In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.