Boolean
A boolean is a datatype which only has two possible states:
truefalse
For example, we could use a boolean to represent whether a light switch is on or off.
Declaring and initializing booleans
Section titled “Declaring and initializing booleans”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.
Uses of booleans
Section titled “Uses of booleans”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!