AP Computer Science Java: Lesson 2.2
Conditional Statements - Relational Operators


Lesson 2.2 - Relational Operators

Purpose: To learn how write Java conditions using relational operators

Conditions
The condition of an if statement must be capable of returning a value of TRUE or FALSE. Additionally, the condition involves making a comparison between one value or expression and another value or expression. Such comparisons require the use of relational operators. Below is a list of Java's relational operators.

The Relational Operators of Java
Operator
Meaning
Example in if
==
is equal to
if(x == 3)
!=
is not equal to
if(x != 3)
<
is less than
if(x < 3)
>
is greater than
if(x > 3)
<=
is less than or equal to
if(x <= 3)
>=
is greater than or equal to
if(x >= 3)

Equals vs. Is Equal To One of the first questions asked is what is the difference between = and ==? The difference is that = is used to assign a value to a variable, whereas  == is used to determine if a variable contains a value. If a single equal sign is used in the condition, it actually assigns the value to the variable. This is not the intended result and will cause the program to execute incorrectly.

© DanShuster.com