In this Java Tutorial, you will learn about decision making statements, different decision making statements in Java, and how to use these decision making statements to implement a given conditional logic in programs, with examples.
Java Decision Making
Decision Making Statements are those that let us execute a block of code based on a condition. This helps us to implement decision making logic in our Java applications.
Java supports the following conditional statements.
- If statement
- If-Else statement
- If-Else-If statement
- Switch statement
Now, let us go through each of these decision making statements with examples.
if statement
If a condition is satisfied, then pick a course of action, else continue with the subsequent statements. For an example, if(you wake up before 11am), then go for the breakfast in fridge.
In the following example, we are checking the condition that if x
is positive, and printing a message to console.
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.
Refer Java If Statement tutorial for syntax and more examples.
if-else statement
If a condition is satisfied, then pick a course of action, else pick another course of action. For an example, if(you wake up before 11am), then go for the breakfast in fridge. else trash it.
In the following example, we are checking the condition that if x
is even. We also have an else block accompanying if block.
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.
Refer Java If-Else tutorial for syntax and more examples.
if-else-if statement
In if-else-if statement, the first if-condition is checked, it not true, then the second if-condition is checked, and so on. When if-condition evaluates to true, the corresponding block of code is executed, and the control comes out of if-else-if statement.
If none of the conditions in if-else-if ladder is true, then the else-block executes, if there is one.
In the following example, we are checking if x
is positive, negative, or zero using if-else-if statement.
Main.java
public class Main {
public static void main(String[] args) {
int x = 10;
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.
You can change the value of variable “time_hours” and play around with the code for better understanding of the execution flow.
Refer Java If-Else-If Statement tutorial for syntax and more examples.
switch-case statement
Switch actually depends on the state of a single variable or state of an expression. For instance, if you consider the day of week as a variable, in case of Monday, curse yourself and go to work, in case of Tuesday, curse one more time and go to work, in case of Saturday, give yourself some credit for bearing the previous five days and treat yourself the best.
In the following example, we execute one of the case-blocks based on the value of x.
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.
If any of the case doesn’t match, default block gets executed. break; statement should be given as the last statement of each case block. If not, the default would execute after the execution of case block.
Refer Java Switch Statement tutorial for syntax and examples.
In Java Decision making, out of all the conditions or cases, there could be only one condition that could be satisfied, and when the condition is satisfied, the course of action intended for that condition is executed.
Conclusion
In this Java Tutorial, we have learnt about Decision Making Statements in Java Programming Language with example programs.