Boolean variables and relational operators in Java

Boolean Variables and Relational Operators in Java: Video Lecture 6

A boolean variable is capable of holding either “true” or “false.” A computer program may use one bit only to store the status “true” or “false.” You might already know that the smallest memory unit of the computer is a “bit.” Therefore, a boolean variable uses as little memory as possible to store the information “true” or “false.” Generally, “true” is stored as “1.” “False” is stored as zero (“0”). Anyway, in programming, there are situations when you will want your program to remember some binary decisions (yes/no decisions.) At that time, binary variables become handy. The conclusion whether a number is greater than another number, or smaller than another number, so and so forth can be stored in a boolean variable.

Comparisons between numbers or variables are frequent in programming. That is, you will face situations where you will need to check if one number is smaller than another number, or if a number is equal to or not equal to another number, so and so forth. In mathematics, such comparisons are called logical conditions. In Java, there are six logical operators to work with logical conditions or to compare variables. The video lecture below provides further details with easy-to-follow programming examples.

The Video: Boolean Variables and Relational Operators in Java

The video explains boolean variables and logical conditions (relational operators). The examples we used in the video are suitable for beginners. Overall, the video will help a beginner to study boolean variables as well as relations operators.

Boolean variables and relational operators in Java.

The Program Used in the Video

We wrote the following program in the video. Please save the content in a file named BoolTest.java before compiling and running it. Although we are providing the code here as a reference, we suggest that you type the program yourself while watching the video. The process of programming involves typing and thinking about the problem simultaneously. This is why it is best if you write the code yourself.

import java.util.Scanner;
class BoolTest{
    public static void main(String[] args){
        boolean b;
        int x;
        int y;
        Scanner scan = new Scanner (System.in);
        System.out.println("Enter the 1st number");
        x=scan.nextInt();
        System.out.println("Enter the 2nd number");
        y=scan.nextInt();
        
        b= (x==y);
        System.out.println(b);
        
    }
}

Exercises

Please do not hesitate to send us your code via the comments section below. We will be happy to provide you feedback.

  1. Write a program that can say whether it is a hot day or not. Ask the user for the temperature (in Ferenhite) of the day. Print “true” if the temperature the user provides is higher than or equal to 90-degree Ferenhite. Print “false”, otherwise. Please do not use if-then-else for this program.
  2. Write a program that can say whether a number is odd or even. Ask for an integer from the user. Print “true” if the user enters an even number and “false” if the user enters an odd number. Please do not use if-then-else for this program. Hint: You will need to use the remainder (%) arithmetic operator.

Additional Resource

The following Java documentation covers relational and conditional operators. It uses if-then-else, which we have not covered yet in our videos. As a result, the link below might not make a whole lot of sense at this point. Still, we are providing it here as a standard reference because it is a part of Oracle Java documentation. The content of the following link will make sense after a few more video lectures.

Equality, Relational, and Conditional Operators

Comment

We will come back soon with the next topic. To stay in touch, please subscribe to Computing4All.com and subscribe to our YouTube channel. Enjoy!

Transcription of the Audio of the Video

Hi,
I am Dr. Shahriar Hossain. Today, I am going to explain the concept of Boolean variables. Boolean variables are capable of holding conditions. In a computer, a condition or a status is either “true” or false.

Logical conditions (to be used with relational operators)

A “condition” in Java refers to logical conditions of mathematics. For example, 10>5, or 6<10. Other than the “greater than” or the “smaller than” symbol, there are several other logical conditions. For example, equal to (==), not equal to (!=) greater than or equal to (>=), smaller than or equal to (<=) .

Relational operators

Here is a complete list of logical conditions
[Show on screen]
• a < b (a is Less than b)
• a <= b (a is Less than or equal to b)
• a > b (a is greater than b)
• a >= b (a is greater than or equal to b)
• a == b (a is equal to b)
• a != b (a is not equal to b)
[End of- Show on screen]

Equality comparison

Java recognizes equality comparison of two variables with two equals symbol (==). If you provide one equal symbol, it is considered as an assignment. Again, one equal symbol means assignment, that is, the right side of the assignment will be copied to the variable in the left side. Two consecutive equal symbols refer to “equality comparison” between two variables or numbers.

Each of these logical conditions results in either “true” or “false”. As a result, a Boolean variable is capable of remembering or holding the result of each of these logical conditions.

Boolean variables to store the results of relational operations

As I said earlier, a Boolean variable is a variable that can hold either true or false. In a computer, “true” refers to 1, and “false” refers to zero. In an earlier video, we explained data types and mentioned the ranges of number variables like integer, double, byte, so and so forth. The range of a Boolean variable is just zero and one, nothing else. Boolean is a data type that requires the smallest memory size, just one small bit. As a result, it can only hold either one or zero, in a small bit size.

Today we will see how we can declare a Boolean variable and what we can do with it. In this video, we are learning logical conditions as well. Logical conditions are comparisons. Comparisons will help us when we will learn conditional statements like if-then-else in a later video.

I will share my screen with you in a moment.

Starting of the code

My class name is BoolTest.java. I am writing my main methods, inside which I have declared a boolean variable named b. I also declare two more variables, x, and y, of integer type. I will ask the user to enter two numbers for x, and y. Therefore, I prepare my Scanner object named scan. I write code to get input for x. I also write code to get input for y.

At this point, I want to keep the information if x is greater than y in the boolean variable. Notice that the answer “if x is greater than y” is either yes, or now. Therefore, for the answer, you just need a Boolean variable because the Boolean variable has the capability to hold true or false, which is “yes” or “no” in this case.

If I type x>y the value is either true or false. If the content in x is greater than the content in y, then the value will be true, otherwise, the value will be false. I assign the value in the right side of the assignment operator to the variable I have on the left side. The variable I have on the left side is b, which is a Boolean variable.

Now let us print the status of the Boolean variable b, using a System.out.println statement. Save the file, compile, and run it. The program ask for the first number. The user enters 10. The program asks for the second number, the user enters 20. The program immediately answers false. Notice that the program only answers if the first number if greater than the second number the user has entered. Since 10 is less than 20, the statement “the first number is greater than the second number” is false. The program has printed false.

Running the program

Let us run the program again. The program asks for the first number. The user enters 50. The program asks for the second number. The user enters 10. Notice that the first number is greater than the second number. The program should now say true. This is because the first number is greater than the second number, and the program is checking if the first number, x, is greater than the second number, y.

Let us do another check now. Let us change the greater than symbol to smaller than. That is the program should output true if the first number is smaller than the second number. Save the file, compile it, and run it. The user enters 20 and 25 as the first and the second number. Since the first number, 20 which is x, is smaller than the second number, 25, which is y, the program outputs “true” now. That is, the program now checks if the first number is smaller than the second number.

I am quickly showing another example. Where you can see that the first number is 30 and the second number the user entered is 10. The program outputs false because the first number 30 is not smaller than the second number 10.

Changing the code with equality (==)

Now, we are going to examine another comparison operation, which is whether the content of two variables are equal or not. As I said earlier, the equality comparison between two variables is accomplished by two equal symbols. Therefore, if we write x two equals (==) y, the right side of the assignment operator should be true only when x is equal to y, otherwise, it should be false. That is, b will be true if x and y are equal. b will be false if x and y are unequal.

Sometimes, my students get confused because this particular line looks a bit weird due to three equals symbol. To reduce the confusion you can put parentheses on the right side of the assignment operator. The parentheses will not change anything. You can put them for clarity. I will skip them in my code. Let us save the file, compile it, and run it.

Running the code

10 and 20 are not equal. Therefore, the program outputs false.
20 and 10 are not equal. Thus, the program outputs false.
The user enters 10 and 10 as the first and the second number. The program outputs true now, which you expected because you used equality comparison in your code.

Let me quickly show you that putting those parentheses will not change the correctness of the program.

Notice that my program prints the same output, which is “true”, for equal inputs.

Concluding remarks regarding boolean variables and relational operators

The main idea of the video today is to familiarize you with the concept of Boolean variables and logical conditions. Over time, we will see more complex logical conditions, as well as conditional statements. Please practice what we have discussed today. For exercises, please visit the link provided in the description. Subscribe to this YouTube channel and to our website Computing4All.com. We do not assume that you know anything about programming. Our video lectures, so far, are prepared for beginners regardless of their backgrounds. Please start from the beginning of this lecture series if you are entirely a new learner.

See you in a few days in the next video lecture. Meantime, please do not hesitate to send us any question you may have, through the comments section below, or via the contact us form on our website Computing4All.com. Thank you very much for watching the video.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *