0% found this document useful (0 votes)
4 views1 page

Java Ternary Operator Example

The document discusses using the ternary operator to perform conditional checks in a shorthand way, where if the condition is true the result before the ? is returned, and if false the result after the : is returned. An example program is given to check if a number is even or odd using ternary operator.

Uploaded by

Puspak Dasgupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Ternary Operator Example

The document discusses using the ternary operator to perform conditional checks in a shorthand way, where if the condition is true the result before the ? is returned, and if false the result after the : is returned. An example program is given to check if a number is even or odd using ternary operator.

Uploaded by

Puspak Dasgupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using Ternary Operator

We can also use ternary operator (? :) to perform the task of if...else statement. It is a
shorthand way to check the condition. If the condition is true, the result of ? is
returned. But, if the condition is false, the result of : is returned.

Example:

1. public class IfElseTernaryExample {


2. public static void main(String[] args) {
3. int number=13;
4. //Using ternary operator
5. String output=(number%2==0)?"even number":"odd number";
6. [Link](output);
7. }
8. }

You might also like