# Boolean expressions and operations

Boolean expressions
1. Always result in a boolean value (either true or false)

2. Are used to resolve logic questions, such as determining if two variables have the same value?

3. May contain several different operators?

Comparison operators
1. Used to compare the values of two variables or constants

2. Require that the two operands be either both numeric (including char) or both boolean. A numeric value cannot be compared to a boolean value.

3. Frequently appear in if, for, while, and do-while statements (to be covered later)

Operator
Operation
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal
!= Not equal

Example:
This program reads two numeric values from the user and displays the result of several comparisons involving the two numbers.

import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Variables for holding two numeric values entered by the user

int x;
double y;

// Prompt for and read the two numeric values

System.out.print(“First number (integer): “);
Scanner Keyboard=new Scanner(System.in);
int x;
x=Keyboard.nextInt(();
System.out.print(“Second number (floating-point): “);
Scanner Keyboard=new Scanner(System.in);
double y;
y=Keyboard.nextDouble();

// Display information comparing the two values

System.out.println(” ” + x + ” < ” + y + ” is ” + (x “<” y));
System.out.println(” ” + x + ” <= ” + y + ” is ” + (x <= y));
System.out.println(” ” + x + ” > ” + y + ” is ” + (x > y));
System.out.println(” ” + x + ” >= ” + y + ” is ” + (x >= y));
System.out.println(” ” + x + ” == ” + y + ” is ” + (x == y));
System.out.println(” ” + x + ” != ” + y + ” is ” + (x != y));
}
}

Scroll to Top