-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path012-Ternary-Operator.java
More file actions
58 lines (48 loc) · 2.13 KB
/
Copy path012-Ternary-Operator.java
File metadata and controls
58 lines (48 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* ============================================================================
* Program Name : Ternary Operator
* File Name : 012-Ternary-Operator.java
* Class Name : TernaryOperator
*
* Description:
* This program demonstrates the use of the ternary operator in Java.
* The ternary operator is a shorthand alternative to the if-else statement.
* It evaluates a condition and returns one of two values depending on
* whether the condition is true or false.
*
* Syntax:
* condition ? expression1 : expression2;
*
* Objective:
* - Understand the ternary operator in Java.
* - Learn how to replace simple if-else statements using the ternary operator.
* - Display the result based on a condition.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
public class TernaryOperator {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Declare and initialize an integer variable.
int number = 25;
// Use the ternary operator to determine whether the number is even or odd.
String result = (number % 2 == 0) ? "Even Number" : "Odd Number";
// Print the given number.
System.out.println("Number : " + number); // Output: Number : 25
// Print the result returned by the ternary operator.
System.out.println("Result : " + result); // Output: Result : Odd Number
// Declare and initialize two integer variables.
int firstNumber = 40;
int secondNumber = 60;
// Use the ternary operator to find the greater number.
int greaterNumber = (firstNumber > secondNumber) ? firstNumber : secondNumber;
// Print both numbers.
System.out.println("First Number : " + firstNumber); // Output: First Number : 40
System.out.println("Second Number : " + secondNumber); // Output: Second Number : 60
// Print the greater number.
System.out.println("Greater Number: " + greaterNumber); // Output: Greater Number: 60
}
}