FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/012-Ternary-Operator.java at main · shaikbasha-dev/Java · GitHub
shaikbasha-dev
/
Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
07-Java-Programs
/
012-Ternary-Operator.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (48 loc) · 2.13 KB
Breadcrumbs
Java
/
07-Java-Programs
/
012-Ternary-Operator.java
Copy path
File metadata and controls
58 lines (48 loc) · 2.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
}
}
Back
|
FazBrowse Home
|
New Git URL