- Relational Operators
Relational operators are used to compare two values and return a boolean result (true
or false
). These operators compare the values based on the relationship between them.
Here are the most common relational operators:
-
==
: Equality operator. Returnstrue
if both operands are equal. -
!=
: Not equal operator. Returnstrue
if both operands are not equal. -
>
: Greater than operator. Returnstrue
if the left operand is greater than the right operand. -
<
: Less than operator. Returnstrue
if the left operand is less than the right operand. -
>=
: Greater than or equal to operator. Returnstrue
if the left operand is greater than or equal to the right operand. -
<=
: Less than or equal to operator. Returnstrue
if the left operand is less than or equal to the right operand. - Logical Operators
Logical operators are used to perform logical operations on boolean values and return boolean results. They are mainly used in control flow and conditional statements.
-
&&
: Logical AND operator. Returnstrue
if both operands aretrue
. -
||
: Logical OR operator. Returnstrue
if at least one operand istrue
. -
!
: Logical NOT operator. Reverses the boolean value of the operand. - Conditional Operator
The conditional operator, also known as the ternary operator, is a shorthand for an if-else
statement. It evaluates a condition and returns one value if the condition is true
and another value if the condition is false
.
The syntax for the conditional operator is:
For example:
int a = (x > y) ? 5 : 10; // if x is greater than y, a is set to 5, otherwise to 10