Ternary Operator
How to Shorten an If-Else Statement in Java
The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:
condition ? expression_for_true : expression_for_false
First, we define a condition, such as 10 > 5, followed by a question mark ?. If the condition is true, the true expression is executed, for example System.out.println("That's true");. If the condition is false, the false expression runs instead, such as System.out.println("That's not true");.
Letβs look at a more practical example:
Main.java
12345678package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }
You may have noticed that the ternary operator is used inside the System.out.println() statement. This is its main advantageβit helps reduce the amount of code, especially in output statements.
The ternary operator can also be used when initializing or returning values. Youβll learn more about returning values in the next section when studying methods.
Hereβs an example of using the ternary operator during initialization:
Main.java
12345678910package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }
Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:
Main.java
1234567891011121314151617181920package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }
The result is the same, but you can see how much space we save by using the ternary operator.
Swipe to start coding
-
Write a code that prints
"The string contains Florida"if the string contains the word Florida, or"Florida is not found"if it does not. -
Use the ternary operator to practice with it.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.63
Ternary Operator
Swipe to show menu
How to Shorten an If-Else Statement in Java
The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:
condition ? expression_for_true : expression_for_false
First, we define a condition, such as 10 > 5, followed by a question mark ?. If the condition is true, the true expression is executed, for example System.out.println("That's true");. If the condition is false, the false expression runs instead, such as System.out.println("That's not true");.
Letβs look at a more practical example:
Main.java
12345678package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }
You may have noticed that the ternary operator is used inside the System.out.println() statement. This is its main advantageβit helps reduce the amount of code, especially in output statements.
The ternary operator can also be used when initializing or returning values. Youβll learn more about returning values in the next section when studying methods.
Hereβs an example of using the ternary operator during initialization:
Main.java
12345678910package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }
Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:
Main.java
1234567891011121314151617181920package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }
The result is the same, but you can see how much space we save by using the ternary operator.
Swipe to start coding
-
Write a code that prints
"The string contains Florida"if the string contains the word Florida, or"Florida is not found"if it does not. -
Use the ternary operator to practice with it.
Solution
Thanks for your feedback!
single