Conditional Operator In Java

Card image cap

Conditional Operators ?

The conditional operator is an operator used in C, C++ and java as well as in other languages also, operator returns one of two values depending on the result of an expression.


Syntax of Conditional Operator ?

(expression 1) ? expression 2 : expression 3

If expression 1 evaluates to true, then expression 2 is evaluated.(or)
If expression 1 evaluates to false, then expression 3 is evaluated instead.


Conditional Operators

Operator Description Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a%b
Card image cap

Example 1:



Output :

Smallest Number is number 1
number3 is not smallest

Example 2:

Output:

Variable is greater then 100
Card image cap

Example 3:


,br>

Output:

Value of b is: 30
Value of b is: 20

Example 4:



Output:

Ans: No
Card image cap