Skip to content

Boolean

A boolean is a datatype which only has two possible states:

  • true
  • false

For example, we could use a boolean to represent whether a light switch is on or off.

We can use the bool class to declare and initialize boolean variables:

bool isLightOn = true;
bool isDoorOpen = false;

Remember that the only two possible values for a boolean are true and false: we couldn’t set isLightOn to 1 or "yes", for example.

Booleans are mainly used for conditional statements - for example, deciding whether to execute a block of code or whether to repeat a loop, depending on a boolean condition.

We’ll cover that when we cover if statements and loops later on!