In this Java tutorial, you’ll learn about decision making statements – the building blocks that allow your program to choose different actions based on conditions. We will explain the various decision making statements available in Java and show you how to use them through simple examples.
Decision Making Statements in Java
Decision making statements let you execute specific blocks of code only when certain conditions are met. This is essential for controlling the flow of your program and implementing logic that reacts to different inputs or situations.
Java supports several types of conditional statements, including:
Let’s explore each of these decision making statements with some simple examples.
1. If Statement
The if
statement checks a condition and executes a block of code if that condition is true.
In the example below, we check if the variable x
is positive. If it is, the program prints a message to the console that the given number is positive.
Main.java
public class Main {
public static void main(String[] args) {
int x = 10;
if (x > 0) {
System.out.println("x is positive.");
}
}
}
Output:
x is positive.
This program uses the if
statement to check if x
is greater than zero. Since x
is 10, the condition is true and the message “x is positive.” is printed.
2. If-Else Statement
An if-else
statement provides an alternative block of code that executes when the condition is false.
In the following example, the program checks if x
is positive. It the number is positive, in the if block, the program prints a message that the number is positive. If the number is not positive, then in the else block, the program prints that the number is not positive.
Main.java
public class Main {
public static void main(String[] args) {
int x = -2;
if (x > 0) {
System.out.println("x is positive.");
} else {
System.out.println("x is not positive.");
}
}
}
Output:
x is not positive.
Here, the if-else
statement checks the condition for x
. Since x
is -2, the condition fails and the else block executes, printing “x is not positive.”
3. If-Else-If Statement
The if-else-if
ladder lets you check multiple conditions in sequence. The program evaluates each condition in order until one of them is true, and then executes the corresponding block. If none of the conditions is met, the else
block runs.
In this example, we check if x
is positive, negative, or zero. We have set x
to 0 so that the final else block executes.
Main.java
public class Main {
public static void main(String[] args) {
int x = 0;
if (x > 0) {
System.out.println("x is positive.");
} else if (x < 0) {
System.out.println("x is negative.");
} else {
System.out.println("x is zero.");
}
}
}
Output:
x is zero.
This program demonstrates the if-else-if
ladder. It checks the value of x
sequentially. Since x
equals 0, none of the first two conditions are met and the else block executes, printing “x is zero.”
4. Switch-Case Statement
The switch
statement evaluates a single expression and executes the matching case block. Each case
is a possible value of the expression. If no case matches, the default
block is executed. Remember to use break;
to prevent fall-through between cases.
In the example below, the value of x
is checked using a switch statement. Since x
is 2, the corresponding case is executed.
Main.java
public class Main {
public static void main(String[] args) {
int x = 2;
switch (x) {
case 0: {
System.out.println("x is zero.");
break;
}
case 1: {
System.out.println("x is one.");
break;
}
case 2: {
System.out.println("x is two.");
break;
}
default: {
System.out.println("default operation.");
}
}
}
}
Output:
x is two.
In this switch-case example, the program evaluates the value of x
and matches it to the corresponding case. Since x
is 2, the case for 2 executes and prints “x is two.” The break;
statement stops further execution of the switch.
Conclusion
In this Java tutorial, we learned about decision making statements, including the if
, if-else
, if-else-if
, and switch
statements. Each of these constructs helps you implement conditional logic in your programs, allowing your code to respond differently based on various conditions. For more detailed syntax and additional examples, refer to the related tutorials linked above.