Relational and logical operators

Relational operators are used to compare values with each other. They return a logical result: true or false. Values that are linked with relational operators are called elementary statements in propositional logic.
A typical example of a Boolean logical operator is the and operation: It always returns true if all operands are also true.

<=less than or equal to5
>=greater than or equal to5
!expression is false1
&&both expressions are true10
||at least one expression is true11

Code Example

This code is a Java program that demonstrates the use of relational and logical expressions. The main method contains a while loop that continues executing as long as the two conditions within the while statement’s parentheses are both true. The first condition is a relational expression that compares the values of the variables a and b using the “not equal to” operator (!=). The second condition is a logical expression that uses the “greater than or equal to” operator (>=) to compare the value of i and the sum of a and b.

Each time the while loop iterates, the value of a is incremented by 1 using the a++ operator. If at any point, either of the conditions in the while statement’s parentheses becomes false, the loop will terminate, and the program will continue executing the line of code immediately following the loop. In this case, the program will print out a message indicating that a is now equal to b.

public class RelationalLogicalExpressions {
	public static void main(String[] args) {
		int a = 0, i = 20, b = 8;
		while (a != b && i >= a + b) {
			System.out.println(a + " is unequal " + b);
		System.out.println(a + " is equal " + b);
0 is unequal 8
1 is unequal 8
2 is unequal 8
3 is unequal 8
4 is unequal 8
5 is unequal 8
6 is unequal 8
7 is unequal 8
8 is equal 8