Introduction
Relational operators help us compare two values to see how they relate to each other, like if one is bigger or smaller.
Relational operators help us compare two values to see how they relate to each other, like if one is bigger or smaller.
value1 < value2 value1 <= value2 value1 > value2 value1 >= value2 value1 == value2 value1 != value2
Use == to check if two values are equal.
Use != to check if two values are not equal.
a is less than b. The result will be true.int a = 5; int b = 10; boolean result = a < b;
age is at least 18 to allow voting.int age = 18; boolean canVote = age >= 18;
x and y are equal.int x = 7; int y = 7; boolean equal = x == y;
temp is not zero (not freezing).int temp = 30; boolean notFreezing = temp != 0;
This program compares the student's score with the passing score using relational operators and prints the results.
public class RelationalOperatorsDemo { public static void main(String[] args) { int score = 75; int passingScore = 60; System.out.println("Score: " + score); System.out.println("Passing Score: " + passingScore); System.out.println("Did the student pass? " + (score >= passingScore)); System.out.println("Is the score less than 50? " + (score < 50)); System.out.println("Is the score equal to 75? " + (score == 75)); System.out.println("Is the score not equal to 100? " + (score != 100)); } }
Relational operators always return a boolean value: true or false.
Use parentheses to make complex comparisons clear.
Relational operators compare two values and return true or false.
They include <, <=, >, >=, ==, and !=.
They are useful for making decisions in programs.
a is greater than or equal to variable b in Java?int x = 5;
int y = 10;
System.out.println(x < y);
System.out.println(x == y);
System.out.println(x != y);
int a = 7;
int b = 3;
if (a <> b) {
System.out.println("Not equal");
}
int a = 4;
int b = 4;
int c = 5;
boolean result = (a <= b) && (c > b);
System.out.println(result);
Pass the test and this pattern moves unknown