Please enable JavaScript to use CodeHS

Chapter 3

Boolean Expressions and if Statements

3.1 Boolean Expressions

How do you write code that tells us whether a user is logged in to our program? Booleans are the solution to these questions.

What are Booleans?

A boolean refers to a value that is true or false. Those are the only values of a boolean expression, and these are useful if you want to check if something is true or false.

Let’s meet the fellow behind the name, “booleans,” George Boole. Mr. Boole was an English-born mathematician, philosopher, and logician. He introduced boolean algebra in The Mathematical Analysis of Logic (1847) and Investigation of the Laws of Thought (1854). Many consider him to be one of the founders of Computer Science.


George Boole

Working with Booleans

Let’s start with an example – create a variable, set it equal to true or false, then print the value.

You first want to declare the variable, loggedIn, and initialize its value.

boolean loggedIn = false;
Plain text

You can similarly set the variable to true instead:

boolean loggedIn = true;
Plain text

You can imagine a boolean variable as a box that can hold only the values true or false. The box below shows the current value of loggedIn:

Notice that you do not need to have quotations around true or false.

The full example looks like:

boolean loggedIn = false;
System.out.println("User logged in?: " + loggedIn);
Plain text

Comparison Operators

Comparison operators allow you to compare two values against one another. A comparison returns a boolean result of either true or false.

The table below lists each of the common comparison operators and their usages:

Operator Usage
> Greater Than
< Less Than
>= Greater Than Or Equal To
<= Less Than Or Equal To
== Equal To
!= Not Equal To

A Basic Comparison

In the following example, you compare two variables x and y. You store the result of this comparison in variable z.

int x = 10;
int y = 8;
boolean z = x > y;
System.out.println(z);
Plain text

What will get printed to the screen? The above comparison, x > y is evaluating if 10 is greater than 8. Because 10 is indeed greater than 8, z is assigned a value of true. Thus, true will get printed to the screen.

More Practice with Comparisons

Let’s get a little more practice. Take a look at the following code segment below. Pay close attention to each comparison and the operator being used.

// Declare some integer variables to use for practice comparisons below.
int a = 3;
int b = 5;
int c = 2;
int d = 3;

// You store the boolean results of each comparison into boolean variables t-z.
boolean t = a > 0; // true
boolean u = a == d; // true
boolean v = d >= b; // false
boolean w = b > c; // true
boolean x = a != d; // false
boolean y = d < = a; // true
boolean z = 4 < = c; // false
Plain text

In addition to integers, it is possible to compare other data types too. In the example below, the boolean variables are storing the results of a comparison between some of the boolean values computed above.

boolean boolComparison1 = t == u; // true
boolean boolComparison2 = t == w; // true
boolean boolComparison3 = t != u; // false
boolean boolComparison4 = x != y; // true
Plain text

Comparison Operators in a Program

Suppose you want to write a program that restricts people under a certain height from riding on a roller coaster. For this particular roller coaster, people who are under 4 feet (48 inches) are not allowed on the ride. How would you do this?

int heightInInches = readInt("How tall are you (in inches)? ");
boolean isTallEnough = heightInInches >= 48;
System.out.println("Can ride on the roller coaster: " + isTallEnough);
Plain text

After getting the potential rider’s height in inches, you do a comparison to ensure that they are over 48 inches. The result of this comparison is then printed out.

Pitfalls

A common mistake is using = when you actually want to use ==. = is used for assignment of variables whereas == is used for comparing the equality of two values.
For example, x = 5 stores the value 5 into the variable x. However, x == 5 tests to see if the value 5 is equal to the variable x and then returns either true or false. They are not the same thing!

Old Enough To Vote

Grade Range

Equality of Strings

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Which of the following is NOT a relational operator?

Exercise: Number Order

Ask the user to enter two ints. Create three boolean variables, as described below:

  • The first should be true if the first number is greater than the second number.

  • The second should be true if the first number is equal to the second number.

  • The third should be true if the first number is less than the second number.

Then print the expressions and the result.

Here is an example:

Enter two numbers: 
10
12
10 > 12: false
10 == 12: false
10 < 12: true
Plain text