A blog for computer science passionates.

Sunday, 15 March 2020

Java: Operators Part-4 Condition?"Ternary":"Decision Making" :)

Hello Friends!!!
This is part 4 for operators in Java, and I hope you can get that Java is rich in the matter of operators and powerful also. In this article, we are going to learn about relational operators and conditional operator.

Relational Operators

The relational operators are used to check the relationship between the two operands. And returns the boolean value, for example, if we want to check the equality between two operands, it checks whether the values are equals or not.

Relational Operators
==
Equal to
a==b
Greater than
a>b
>=
Greater than or equal to
a>=b
Less than
a<b
<=
Less than or equal to
a<=b
!=
Not equal to
a!=b


The above table shows the relational operators. Please note to check the equality we have to use == two equals sign. Single = is used as an assignment operator.

Conditional Operator

This operator is also known as a ternary operator. we can use this operator in the place of if-else. It works with relational operators to check the conditions.
Why it is a ternary operator? the answer is it works with three operands thus it is known as the ternary operator.
Syntax,
condition?expr1:expr2
And even you can check from the syntax there is a condition so it is the conditional operator and expr1 works when a condition is true and expr2 returns when a condition is false. So it is a conditional operator. Let's take an example,

Happy Coding...☺😊

No comments:

Post a Comment