boolean operatoer conditional relational java

2.1 Java | Using Relational Operators & Booleans

Introduction to Selections

Java programs have the power to choose which statements to execute depending on conditions.

Let’s start with a situational example. If you remember our Java program for computing the area of a circle, the program should display an invalid result if a negative radius is used. However, if you add a negative number in place of the radius; you’ll find that the program still works. So how do we force the program to reject a negative number for the radius?

We can use selections statements, which are statements that allow you to choose an action depending on a condition. For our previously mentioned situation, let’s try using the if else selection statements:

Output:

Enter a radius for the circle 5 Circle Radius is 5.0 Circle Area is 78.5

Enter a radius for the circle -5 Incorrect input; radius cannot be negative

Note that other than if else statements, selection statements can also use Boolean expressions (true / false) as conditions:

Relational Operators & Boolean Data Type

The boolean data type allows you declare a variable with either true or false values.

You can compare two values by using Java’s 6 relational (or comparison) operators.

*Note that the relational operator == is not a =. The former is for comparisons, the later is for assignment.

When you use relational/comparison operators, you be given back either a true or false value. For example, the statement below will output a true value:

Know that a Boolean variable is a variable that carries a Boolean value. A variable is declared as Boolean by using the boolean data type. And these boolean variables can hold either a true or false value. For example, boolean lightsOff = false; assigns false to the variable lightsOff.

Know that just like 11, both true and false are literals. Also know that these two Boolean literals are treated as reserved keywords and therefore cannot take the role of identifiers in Java code.

Example: Addition Quiz True or False

Making a Conditional Addition Quiz Return True or False

Purpose: Let’s say that you are developing a program that allows elementary schoolers practice addition. This program is required to randomly generate 2 single digit integers, num1 & num2. These 2 single digit integers are displayed in the form of a question like “What is 4 + 5?”. After an answer is typed and submitted, the program should return feedback to the user as to whether the submission is true or false.

The first thing we need to figure out is how to randomly generate numbers. One way is to use System.currentTimeMillis() % 10 for the first integer and System.currentTimeMillis() /7 % 10 for the second integer.

Then we can check the user’s answer using the equal to == operator to return feedback true or false.

Extra Notes

= not ==

Just a small memory bridge:

Note that the relational operator == is not a =. the former is for comparisons, the later is for assignment.

  • you = bob; –> “I am Bob”
  • you == bob –> “Am I Bob?”

Read More about = not ==

Yoda Code

A common trick to prevent using the assignment operator instead of the comparison operator is to use “Yoda code”, i.e. to put the literal left of the comparison:

Instead of:

use

Should you confuse assignment with comparison, the compiler will complain. [Source]


◄◄◄BACK | NEXT►►►

What's Your Opinion?