In programming, making decisions based on conditions is a fundamental task. Java provides several constructs to handle these decisions, including the if, else if, and else statements, as well as the ternary operator for concise conditional expressions. Understanding how to use these constructs effectively will enhance your ability to write robust and dynamic Java programs.
Conditional statements allow your program to execute different blocks of code based on whether certain conditions are true or false. This is crucial for creating interactive applications that respond to user input, handle various scenarios, and make logical decisions.
In this tutorial, you'll learn how to use the if, else if, and else statements to control the flow of your Java programs. Additionally, we'll explore the ternary operator as a shorthand for simple conditional expressions.
The if statement is used to execute a block of code only if a specified condition evaluates to true. Here's the basic syntax:
1if (condition) {2// Code to be executed if the condition is true3};
Let's start with a simple example where we check if a number is positive.
1public class IfExample {2public static void main(String[] args) {3int number = 5;45if (number > 0) {6System.out.println("The number is positive.");7}8}9}
The number is positive.
number and assign it the value 5.if statement checks if number is greater than 0. Since this condition is true, the code inside the curly braces is executed.number = -5), the code block would not be executed.The else statement allows you to execute a different block of code when the if condition is false. This provides an alternative path for your program.
1if (condition) {2// Code to be executed if the condition is true3} else {4// Code to be executed if the condition is false5}
Here's an example where we check if a number is positive or negative.
1public class IfElseExample {2public static void main(String[] args) {3int number = -3;45if (number > 0) {6System.out.println("The number is positive.");7} else {8System.out.println("The number is negative or zero.");9}10}11}
The number is negative or zero.
number is greater than 0. Since this condition is false, the code inside the else block is executed.else block provides an alternative action when the if condition fails.The else if statement allows you to test multiple conditions in sequence. It's useful when there are more than two possible outcomes.
1if (condition1) {2// Code to be executed if condition1 is true3} else if (condition2) {4// Code to be executed if condition1 is false and condition2 is true5} else {6// Code to be executed if all previous conditions are false7}
Let's check if a number is positive, negative, or zero.
1public class IfElseIfExample {2public static void main(String[] args) {3int number = 0;45if (number > 0) {6System.out.println("The number is positive.");7} else if (number < 0) {8System.out.println("The number is negative.");9} else {10System.out.println("The number is zero.");11}12}13}
The number is zero.
number > 0 is true, it prints "The number is positive."number < 0. Since this condition is also false, it moves to the else block.else block executes when all previous conditions are false.The ternary operator provides a concise way to write simple conditional expressions. It's a shorthand for an if-else statement.
1result = (condition) ? expression1 : expression2;
Let's use the ternary operator to determine if a number is even or odd.
1public class TernaryExample {2public static void main(String[] args) {3int number = 4;45String result = (number % 2 == 0) ? "Even" : "Odd";67System.out.println("The number is " + result);8}9}
The number is Even.
number % 2 == 0. If true, it assigns the string "Even" to result.result.Let's create a complete program that calculates and displays a student's grade based on their score.
1public class GradeCalculator {2public static void main(String[] args) {3int score = 85;45if (score >= 90) {6System.out.println("Grade: A");7} else if (score >= 80) {8System.out.println("Grade: B");9} else if (score >= 70) {10System.out.println("Grade: C");11} else if (score >= 60) {12System.out.println("Grade: D");13} else {14System.out.println("Grade: F");15}16}17}
Grade: B
else if statements to handle different scenarios.| Concept | Description |
|---|---|
| If Statement | Executes code if a condition is true. |
| Else Statement | Executes code if the preceding conditions are false. |
| Else If Statement | Tests additional conditions in sequence after an if or another else if. |
| Ternary Operator | Provides a concise way to write simple conditional expressions. |
In the next tutorial, we'll explore the switch statement, which is used for executing one block of code among many alternatives. This will allow you to handle multiple conditions in a more organized manner.
To continue learning, compile and run the examples provided in this tutorial using the following commands:
$ javac IfExample.java$ java IfExample$ javac IfElseExample.java$ java IfElseExample$ javac IfElseIfExample.java$ java IfElseIfExample$ javac TernaryExample.java$ java TernaryExample$ javac GradeCalculator.java$ java GradeCalculator
Happy coding!