Booleans
What are booleans and how to use them to control your program's flow
Booleans are a special variable data type that can only store two possible values: 1 and 0. These values are intended to represent the two truth values in logic: True and False.
Note: It’s only storing the numbers 1 and 0, but because it’s basically only ever used to represent True and False, most languages offer programmers the ability to use 1/True and 0/False interchangeably when referencing or modifying a boolean.
To serve this purpose, a boolean variable is best declared like so:
Code:
our_boolean = True
Code:
boolean b = true;
Code:
bool b = true;
In many languages, booleans can also be declared by setting it equal to 1 or 0, but it is best practice to use true and false to not confuse booleans with integers.
Like the integer data type you saw in the “Variables” article, Booleans can also be modified and manipulated in various ways. Here are some examples:
  • Booleans can be declared as equal to some statement the computer can check is true or false. In the following example, we ask the program if 1 is greater than 2.
Code:
b = 1 > 2
print(b)
Output:
> False
Code:
boolean b = 1 > 2;
System.out.println(b);
Output:
> false
Code:
bool b = 1 > 2;
cout << b;
Output:
> 0
(Note in this case 0 represents false)
  • You can also use variables in the statement that the computer checks is true or false.
Code:
a = 2
b = 1
c = a > b
print(c)
Output:
> True
Code:
int a = 2;
int b = 1;
boolean c = a > b;
System.out.println(c);
Output:
> true
Code:
int a = 2;
int b = 1;
bool c = a > b;
cout << c;
Output:
> 1
(Note in this case 1 represents true)
You may be wondering how we check two variables are equal to each other. We can use the
==
operator to do this, which checks if the value on the left of the operator is equal to the value on the right of the operator. Notice it uses two equal signs, which helps the computer distinguish between whether we are trying to check if two variables are equal vs whether we are trying to modify a variable’s value.
Code:
a = 1
b = 1
c = (a == b)
print(c)
Output:
> True
Code:
int a = 1;
int b = 1;
boolean c = (a == b);
System.out.println(c);
Output:
> true
Code:
int a = 1;
int b = 1;
bool c = (a == b);
cout << c;
Output:
> 1
(Note in this case 1 represents true)
Common Mistake: Accidentally using the
=
operator instead of the
==
operator to check if two values are equal is one of the most common beginner mistakes. Make sure you’re using the right operator for what you want to do!
Conditionals
Conditional statements are statements that can control the flow of a program when used in conjunction with booleans. The most common conditional statement is the “if-statement,” which has three components:
  1. The if-statement itself
  2. The boolean that determines if the enclosed code will run
  3. The enclosed code that will run if the boolean is true
For example, this code will print the variable
b
only if it is true because the print statement is enclosed in the body of the if-statement:
  • When b is true:
Code:
b = True
if b:
    print(b)
Output:
> True
Code:
boolean b = true;
if(b) {
    System.out.println(b);
}
Output:
> true
Code:
bool b = true;
if(b) {
    cout << b;
}
Output:
> 1
  • When b is false:
Code:
b = False
if b:
    print(b)
Output:
>
Code:
boolean b = false;
if(b) {
    System.out.println(b);
}
Output:
>
Code:
bool b = false;
if(b) {
    cout << b;
}
Output:
>
Note: Python doesn’t use brackets and instead uses indentation to indicate that a body of code is enclosed in an if-statement. The lack of brackets is one of the hallmarks of Python that make it an easily identifiable language.
“If-statement”s can also be expanded into “if-else-statement”s, which add an additional level of control. The body of code enclosed in the else component only runs if the given boolean is false.
  • When b is true:
Code:
b = True
if b:
    print("b is true!")
else:
    print("b is false!")
Output:
> b is true!
Code:
boolean b = true;
if(b) {
    System.out.println("b is true!");
} else {
    System.out.println("b is false!");
}
Output:
> b is true!
Code:
bool b = true;
if(b) {
    cout << "b is true!";
} else {
    cout << "b is false!";
}
Output:
> b is true!
  • When b is false:
Code:
b = False
if b:
    print("b is true!")
else:
    print("b is false!")
Output:
> b is false!
Code:
boolean b = false;
if(b) {
    System.out.println("b is true!");
} else {
    System.out.println("b is false!");
}
Output:
> b is false!
Code:
bool b = false;
if(b) {
    cout << "b is true!";
} else {
    cout << "b is false!";
}
Output:
> b is false!
Let’s apply our repertoire of variables, booleans, and conditionals to write a short program that tells you what to wear.
Imagine you want to wear a t-shirt if the temperature is >70 degrees Fahrenheit, and otherwise, you want to wear a jacket. Your code for this scenario might look like this:
Code:
temperature = 80 # replace 80 with today’s temperature
if temperature > 70:
    print("Wear a t-shirt")
else:
    print("Wear a jacket")
Output:
> Wear a t-shirt
Code:
int temperature = 80; // replace 80 with today's temperature
if(temperature > 70) {
    System.out.println("Wear a t-shirt");
} else {
    System.out.println("Wear a jacket");
}
Output:
> Wear a t-shirt
Code:
int temperature = 80; // replace 80 with today's temperature
if(temperature > 70) {
    cout << "Wear a t-shirt";
} else {
    cout << "Wear a jacket";
}
Output:
> Wear a t-shirt
This program executes perfectly, but the issue is we also don’t want to wear a jacket for all temperatures ≤70 degrees. We instead want to wear a coat if the temperature is <40 degrees. Here is the catch: we now have three different ranges where we will be wearing various things depending on the temperature (<40, ≥40 but ≤70, >70). How can we represent three different values in the boolean we give the if-else-statement when a boolean can only store two different values? Where do we define the 3rd body of code since an if-else statement only has room for two bodies of code?
To address this issue, we can use a nested if-else-statement. A nested if-else-statement is an if-else-statement enclosed within another if-else statement. We can use this in our scenario to address our problem in 2 steps:
  1. First, determine if the temperature is >70 degrees, and if it is, wear a t-shirt.
  2. If it isn’t, we either need to wear a jacket or coat, so we use another if-statement to figure out if we will wear a jacket or coat
This is what that logic would look like in code:
Code:
if temperature > 70:
    print("Wear a t-shirt")
else:
    if temperature < 40:
        print("Wear a coat")
    else:
        print("Wear a jacket")
Code:
if(temperature > 70) {
    System.out.println("Wear a t-shirt");
} else {
    if(temperature < 40) {
        System.out.println("Wear a coat");
    } else {
        System.out.println("Wear a jacket");
    }
}
Code:
if(temperature > 70) {
    cout << "Wear a t-shirt";
} else {
    if(temperature < 40) {
        cout << "Wear a coat";
    } else {
        cout << "Wear a jacket";
    }
}
Let’s test our code with unit tests, a type of software testing that checks all of the units of our code are working as intended. In this case, our units would be the temperature ranges, so we can use temperatures 20, 60, and 80 to test all 3 of our units.
  • When the temperature is 20:
Code:
temperature = 20
if temperature > 70:
    print("Wear a t-shirt")
else:
    if temperature < 40:
        print("Wear a coat")
    else:
        print("Wear a jacket")
Output:
> Wear a coat
Code:
int temperature = 20;
if(temperature > 70) {
    System.out.println("Wear a t-shirt");
} else {
    if(temperature < 40) {
        System.out.println("Wear a coat");
    } else {
        System.out.println("Wear a jacket");
    }
}
Output:
> Wear a coat
Code:
int temperature = 20;
if(temperature > 70) {
    cout << "Wear a t-shirt";
} else {
    if(temperature < 40) {
        cout << "Wear a coat";
    } else {
        cout << "Wear a jacket";
    }
}
Output:
> Wear a coat
  • When the temperature is 60:
Code:
temperature = 60
if temperature > 70:
    print("Wear a t-shirt")
else:
    if temperature < 40:
        print("Wear a coat")
    else:
        print("Wear a jacket")
Output:
> Wear a jacket
Code:
int temperature = 60;
if(temperature > 70) {
    System.out.println("Wear a t-shirt");
} else {
    if(temperature < 40) {
        System.out.println("Wear a coat");
    } else {
        System.out.println("Wear a jacket");
    }
}
Output:
> Wear a jacket
Code:
int temperature = 60;
if(temperature > 70) {
    cout << "Wear a t-shirt";
} else {
    if(temperature < 40) {
        cout << "Wear a coat";
    } else {
        cout << "Wear a jacket";
    }
}
Output:
> Wear a jacket
  • Finally, when the temperature is 80:
Code:
temperature = 80
if temperature > 70:
    print("Wear a t-shirt")
else:
    if temperature < 40:
        print("Wear a coat")
    else:
        print("Wear a jacket")
Output:
> Wear a t-shirt
Code:
int temperature = 80;
if(temperature > 70) {
    System.out.println("Wear a t-shirt");
} else {
    if(temperature < 40) {
        System.out.println("Wear a coat");
    } else {
        System.out.println("Wear a jacket");
    }
}
Output:
> Wear a t-shirt
Code:
int temperature = 80;
if(temperature > 70) {
    cout << "Wear a t-shirt";
} else {
    if(temperature < 40) {
        cout << "Wear a coat";
    } else {
        cout << "Wear a jacket";
    }
}
Output:
> Wear a t-shirt
You can play with all the code we've used in this article on Replit:
Copyright ©2023 Howard County Hour of Code