Decision Making in Java (if, if-else, switch, break, continue, jump)
Last Updated :
16 Apr, 2025
Decision-making statements in Java execute a block of code based on a condition. Decision-making in programming is similar to decision-making in real life. In programming, we also face situations where we want a certain block of code to be executed when some condition is fulfilled.
A programming language uses control statements to control the flow of execution of a program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program. Java provides several control statements to manage program flow, including:
- Conditional Statements: if, if-else, nested-if, if-else-if
- Switch-Case: For multiple fixed-value checks
- Jump Statements: break, continue, return
Types of Decision-Making Statements
The table below demonstrates various control flow statements in programming, their use cases, and examples of their syntax.
Statement
| Use Case
| Example
|
---|
if
| Single condition check
| if (age >= 18)
|
---|
if-else
| Two-way decision
| if (x > y) {…} else {…}
|
---|
nested-if
| Multi-level conditions
| if (x > 10) { if (y > 5) {…} }
|
---|
if-else-if
| Multiple conditions
| if (marks >= 90) {…} else if (marks >= 80) {…}
|
---|
switch-case
| Exact value matching
| switch (day) { case 1: … }
|
---|
break
| Exit loop/switch
| break;
|
---|
continue
| Skip iteration
| continue;
|
---|
return
| Exit method
| return result;
|
---|
1. Java if Statement
The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not.
Syntax:
if(condition) {
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements under it. If we don’t use curly braces( {} ), only the next line after the if is considered as part of the if block For example,
if (condition) // Assume condition is true
statement1; // Belongs to the if block
statement2; // Does NOT belong to the if block
Here’s what happens:
- If the condition is True statement1 executes.
- statement2 runs no matter what because it’s not a part of the if block
if Statement Execution Flow
The below diagram demonstrates the flow chart of an “if Statement execution flow” in programming.

Example: The below Java program demonstrates without curly braces, only the first line after the if statement is part of the if block and the rest code will be execute independently.
Java
// Java program to illustrate
// if statement without curly block
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
// part of if block(immediate one statement
// after if condition)
System.out.println("Inside If block");
// always executes as it is outside of if block
System.out.println("10 is less than 15");
// This statement will be executed
// as if considers one statement by default again
// below statement is outside of if block
System.out.println("I am Not in if");
}
}
OutputInside If block
10 is less than 15
I am Not in if
2. Java if-else Statement
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here, comes the “else” statement. We can use the else statement with the if statement to execute a block of code when the condition is false.
Syntax:
if(condition){
// Executes this block if
// condition is true
}else{
// Executes this block if
// condition is false
}
if-else Statement Execution flow
The below diagram demonstrates the flow chart of an “if-else Statement execution flow” in programming
Example: The below Java program demonstrates the use of if-else statement to execute different blocks of code based on the condition.
Java
// Java program to demonstrate
// the working of if-else statement
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Outputi is smaller than 15
3. Java nested-if Statement
A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
nested-if Statement Execution Flow
The below diagram demonstrates the flow chart of an “nested-if Statement execution flow” in programming.
Example: The below Java program demonstrates the use of nested if statements to check multiple conditions.
Java
// Java program to demonstrate the
// working of nested-if statement
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i < 15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
}
else {
System.out.println("i is greater than 15");
}
}
}
Outputi is smaller than 15
i is smaller than 12 too
4. Java if-else-if ladder
Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that ‘if’ is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. There can be as many as ‘else if’ blocks associated with one ‘if’ block but only one ‘else’ block is allowed with one ‘if’ block.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
if-else-if ladder Execution Flow
The below diagram demonstrates the flow chart of an “if-else-if ladder execution flow” in programming
Example: This example demonstrates an if-else-if ladder to check multiple conditions and execute the corresponding block of code based on the value of I.
Java
// Java program to demonstrate the
// working of if-else-if ladder
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
5. Java Switch Case
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
Syntax:
switch (expression) {
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// more cases…
default:
// code to be executed if no cases match
}
switch Statements Execution Flow
The below diagram demonstrates the flow chart of a “switch Statements execution flow” in programming.

Example: The below Java program demonstrates the use of switch-case statement to evaluate multiple fixed values.
Java
// Java program to demonstrates the
// working of switch statements
import java.io.*;
class Geeks {
public static void main(String[] args)
{
int num = 20;
switch (num) {
case 5:
System.out.println("It is 5");
break;
case 10:
System.out.println("It is 10");
break;
case 15:
System.out.println("It is 15");
break;
case 20:
System.out.println("It is 20");
break;
default:
System.out.println("Not present");
}
}
}
- The expression can be of type byte, short, int char, or an enumeration. Beginning with JDK7, the expression can also be of type String.
- Duplicate case values are not allowed.
- The default statement is optional.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statements are necessary without the break keyword, statements in switch blocks fall through.
- If the break keyword is omitted, execution will continue to the next case.
6. jump Statements
Java supports three jump statements: break, continue and return. These three statements transfer control to another part of the program.
- Break: In Java, a break is majorly used for:
- Terminate a sequence in a switch statement (discussed above).
- To exit a loop.
- Used as a “civilized” form of goto.
- Continue: Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop’s end. The continue statement performs such an action.
Jump Statements Execution Flow
The below diagram demonstrates the flow chart of a “jump Statements execution flow” in programming.

Example: The below Java Program demonstrates how the continue statement skip the current iteration when a condition is true.
Java
// Java program to demonstrates the use of
// continue in an if statement
import java.util.*;
class Geeks {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is even
// skip and continue
if (i % 2 == 0)
continue;
// If number is odd, print it
System.out.print(i + " ");
}
}
}
Return Statement
The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method.
Example: The below Java program demonstrates how the return statements stop a method and skips the rest of the code.
Java
// Java program to demonstrate the use of return
import java.util.*;
public class Geeks {
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if (t)
return;
// Compiler will bypass every statement
// after return
System.out.println("This won't execute.");
}
}
Comparison of Decision-Making Statements
if-else vs switch-case
The table below demonstrates the difference between if-else and switch-case.
Features
| if-else
| switch-case
|
---|
Use Case
| Suitable for condition-based checks
| Best for exact value matching
|
---|
Readability
| More readable for a few conditions
| More readable and efficient for many cases
|
---|
Performance
| Slower for many checks due to multiple conditions
| Faster and optimized for handling many cases
|
---|
Flexibility
| Supports ranges and complex conditions
| Only supports exact matches of values
|
---|
Best Practices for Decision-Making in Java
- Use switch for multiple exact matches, it improves readability.
- Try avoiding deep nesting.
- Always include default in switch for unexpected cases.
- Prefer if-else for range checks.
Similar Reads
Basics of Java
If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
10 min read
Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers Write Once and Run Anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the
9 min read
Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as comepetitive programming . C++ is a widely popular language among coders for its efficiency, high speed, and dynam
6 min read
In the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Win
5 min read
Java is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand. Java Syntax refers to a set of rules that defines how Java programs are
6 min read
Java is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applicat
6 min read
Understanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is: JDK: Java Development Kit is a software devel
4 min read
JVM(Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one s
7 min read
An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name. Example: public class Test{ public static void main(String[] args) { int a =
2 min read
Variables & DataTypes in Java
In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated. Key Components of Variables in Java: A variable in Java has three components, which are listed below: Data Type: Defines the k
9 min read
The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e. scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about J
6 min read
Java is statically typed and also a strongly typed language because each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be declared with the specifi
15 min read
Operators in Java
Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they p
8 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
10 min read
Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
Operators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
5 min read
In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming. There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level.
6 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
In Java, Jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. Example: arr [][]= { {1,2}, {3,4,5,6},{7,8,9}}; So, here you can check that the number of columns in row1!=row2!=row3. Tha
5 min read
Strings in Java
In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
10 min read
String is a sequence of characters. In Java, objects of the String class are immutable which means they cannot be changed once created. In this article, we will learn about the String class in Java. Example of String Class in Java: [GFGTABS] Java // Java Program to Create a String import java.io.*;
7 min read
The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters. Features of StringBuffer ClassThe key features of StringBuffer
11 min read
In Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations. Declaration: StringBuilder sb = n
7 min read
OOPS in Java
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable. The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
12 min read
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects. Example: Java program to demonstrate how to cre
8 min read
In Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
7 min read
A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read