Core Java: Control Statements in Java — How Java Controls Program Flow
In the previous articles, we learned about:
- Variables and Data Types
- Type Conversion and Type Promotion
- Operators in Java
So far, our Java programs have mostly executed from top to bottom.
But real-world programs don’t always work like that.
Sometimes we need to:
- Make a decision
- Execute something only when a condition is true
- Repeat a piece of code multiple times
- Skip an iteration
- Stop a loop
- Return from a method
This is where Control Statements come into the picture.
What are Control Statements?
Control statements are statements that control the flow or execution of a program.
Normally, Java executes statements one after another:
Statement 1
↓
Statement 2
↓
Statement 3
↓
Statement 4But control statements allow us to change this flow.
For example:
Condition
/ \
true false
↓ ↓
Execute A Execute BJava provides different types of control statements.
Types of Control Statements in Java
Control statements can be broadly divided into three categories:
Control Statements
│
├── 1. Conditional / Decision-Making Statements
│
├── 2. Looping / Iteration Statements
│
└── 3. Jump StatementsLet’s understand each category.
1. Conditional Statements
Conditional statements are used when we want our program to make a decision based on a condition.
For example:
If the person’s age is greater than or equal to 18, they are eligible to vote.
We can represent this using an if statement.
The main conditional statements in Java are:
ifif-elseelse-if- Nested
if switch
if Statement
The if statement executes a block of code only when the given condition is true.
Syntax:
if (condition) {
// code to execute
}Example:
int age = 20;if (age >= 18) {
System.out.println("You are eligible to vote");
}Here:
age >= 18is the condition.
Since 20 >= 18 is true, Java executes the code inside the if block.
Output:
You are eligible to voteif-else Statement
What if we want to execute one block when the condition is true and another block when it is false?
That’s where if-else comes in.
Example:
int age = 16;if (age >= 18) {
System.out.println("Eligible to vote");
} else {
System.out.println("Not eligible to vote");
}Output:
Not eligible to voteThe flow looks like:
age >= 18?
/ \
true false
↓ ↓
Eligible Not EligibleSo:
ifhandles the true condition, whileelsehandles the false condition.
else-if Statement
Sometimes we have multiple conditions to check.
For example, consider student marks.
int marks = 85;if (marks >= 90) {
System.out.println("Grade A+");
} else if (marks >= 80) {
System.out.println("Grade A");
} else if (marks >= 70) {
System.out.println("Grade B");
} else if (marks >= 60) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}Here Java checks the conditions from top to bottom.
Since:
85 >= 90 → false
85 >= 80 → trueJava executes:
Grade AOnce Java finds a condition that is true, it executes that block and skips the remaining else-if conditions.
Nested if
An if statement inside another if statement is called a nested if.
For example:
int age = 25;
boolean hasLicense = true;if (age >= 18) { if (hasLicense) {
System.out.println("You can drive");
}}
Here we have two conditions:
Age should be 18 or above
↓
Then check
↓
Does the person have a license?Nested if statements can be useful when one condition depends on another.
However, if the conditions are simple, sometimes using the logical && operator is cleaner:
if (age >= 18 && hasLicense) {
System.out.println("You can drive");
}switch Statement
The switch statement is useful when we want to compare one value against multiple possible values.
For example:
int day = 2;switch (day) { case 1:
System.out.println("Monday");
break; case 2:
System.out.println("Tuesday");
break; case 3:
System.out.println("Wednesday");
break; default:
System.out.println("Invalid day");
}
Output:
TuesdayHere Java checks the value of day.
Since:
day = 2Java executes:
case 2Why Do We Use break in Switch?
This is an important point.
Consider:
int day = 2;switch (day) { case 1:
System.out.println("Monday"); case 2:
System.out.println("Tuesday"); case 3:
System.out.println("Wednesday");
}
Without break, Java can continue executing the following cases after finding a match.
This is called fall-through.
Usually, we don’t want that.
That’s why we use:
break;Example:
case 2:
System.out.println("Tuesday");
break;The break tells Java:
“We found our case. Stop executing the switch.”
What is default?
The default block executes when none of the cases match.
Example:
int day = 10;switch (day) { case 1:
System.out.println("Monday");
break; case 2:
System.out.println("Tuesday");
break; default:
System.out.println("Invalid day");
}
Since there is no case 10, Java executes:
Invalid dayThink of default as the else of a switch statement.
2. Looping Statements
Now let’s say we want to execute the same piece of code multiple times.
For example, suppose we want to print:
Hellofive times.
We could write:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");But this is repetitive.
Instead, we can use a loop.
Looping statements allow us to execute a block of code repeatedly as long as a condition is satisfied.
The main loops in Java are:
forwhiledo-while
for Loop
The for loop is commonly used when we know how many times we want to execute something.
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Hello");
}Output:
Hello
Hello
Hello
Hello
HelloLet’s understand the structure:
for (initialization; condition; update) {
// code
}In our example:
initialization → int i = 1
condition → i <= 5
update → i++The flow is:
i = 1
↓
i <= 5?
↓
Execute code
↓
i++
↓
Check condition againThis continues until the condition becomes false.
while Loop
A while loop executes a block of code as long as the condition is true.
Get Aishu Reddy’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
int i = 1;while (i <= 5) {
System.out.println(i);
i++;
}Output:
1
2
3
4
5The important thing about a while loop is that the condition is checked before the loop body executes.
So if the condition is false initially, the loop may not execute even once.
do-while Loop
The do-while loop is slightly different.
Here, the code executes at least once, because the condition is checked after the loop body.
Example:
int i = 1;do {
System.out.println(i);
i++;
} while (i <= 5);Output:
1
2
3
4
5The flow is:
Execute code
↓
Update value
↓
Check condition
↓
true → repeat
false → stopDifference Between while and do-while
Consider this example:
int i = 10;while (i < 5) {
System.out.println("Hello");
}The condition is false from the beginning.
So nothing is printed.
Now look at:
int i = 10;do {
System.out.println("Hello");
} while (i < 5);Output:
HelloEven though the condition is false, the code runs once.
Therefore:
while
→ Condition is checked firstdo-while
→ Code executes first, condition is checked later3. Jump Statements
The third category of control statements is jump statements.
Jump statements allow us to change the normal flow of execution.
The main jump statements are:
breakcontinuereturn
Let’s understand them.
break Statement
We already saw break in the switch statement.
break is also commonly used inside loops.
It immediately terminates the loop.
Example:
for (int i = 1; i <= 10; i++) {if (i == 5) {
break;
} System.out.println(i);
}
Output:
1
2
3
4When i becomes 5, the break statement executes.
The loop stops immediately.
So:
break= Stop the loop completely.
continue Statement
The continue statement is different from break.
Instead of stopping the entire loop, continue skips the current iteration and moves to the next one.
Example:
for (int i = 1; i <= 5; i++) {if (i == 3) {
continue;
} System.out.println(i);
}
Output:
1
2
4
5When i becomes 3, Java skips the rest of that iteration.
The loop then continues with 4.
So:
continue= Skip the current iteration.
break vs continue
This is an important difference to remember.
break
↓
Stops the entire loopcontinue
↓
Skips the current iteration
and continues with the next iterationFor example:
break;means:
“I’m done with this loop.”
Whereas:
continue;means:
“Skip this one and keep going.”
return Statement
The return statement is used to return a value from a method or to immediately exit a method.
For example:
public static int add(int a, int b) {
return a + b;
}Here:
return a + b;returns the result to whoever called the method.
Example:
int result = add(10, 20);System.out.println(result);Output:
30return can also be used in a void method to exit early:
public static void checkAge(int age) {if (age < 18) {
return;
} System.out.println("Eligible");
}
If age is less than 18, the return statement exits the method immediately.
Complete Classification
Now we can see the complete picture:
Control Statements
|
+-------------+-------------+
| | |
↓ ↓ ↓
Conditional Looping Jump
Statements Statements Statements
| | |
↓ ↓ ↓
if for break
if-else while continue
else-if do-while return
switchThis classification makes it much easier to remember.
Real-World Example
Let’s combine a few concepts.
Suppose we want to check whether students have passed an exam.
int marks = 75;if (marks >= 90) {
System.out.println("Excellent");
} else if (marks >= 60) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}Output:
PassedNow suppose we want to print the result for multiple students:
for (int i = 1; i <= 5; i++) {int marks = i * 20; if (marks >= 40) {
System.out.println("Student " + i + " passed");
} else {
System.out.println("Student " + i + " failed");
}
}
Here we are combining:
for loop
+
if-elseThis is how control statements start becoming useful in real programs.
Control Statements vs Conditional Statements
One thing that is important to understand is that conditional statements and control statements are not the same thing.
Conditional statements are a subcategory of control statements.
Think of it like this:
Control Statements
│
├── Conditional Statements
│ ├── if
│ ├── if-else
│ ├── else-if
│ └── switch
│
├── Looping Statements
│ ├── for
│ ├── while
│ └── do-while
│
└── Jump Statements
├── break
├── continue
└── returnSo when someone says:
“Let’s learn conditional statements.”
They are talking specifically about decision-making.
When someone says:
“Let’s learn control statements.”
They are talking about the broader concept that includes conditions, loops, and jumps.
Quick Summary
Let’s quickly revise everything we learned.
Conditional Statements
Used to make decisions.
if
if-else
else-if
switchLooping Statements
Used to repeat code.
for
while
do-whileJump Statements
Used to change the flow of execution.
break
continue
returnEasy Way to Remember
When writing a Java program, ask yourself:
Do I need to make a decision?
Use:
if / else / switchDo I need to repeat something?
Use:
for / while / do-whileDo I need to stop or skip something?
Use:
break / continueDo I need to exit a method or send a value back?
Use:
returnThat’s the basic idea behind control flow in Java.
What’s Next?
So far, our Core Java journey looks like this:
Java Basics
↓
Variables & Data Types
↓
Type Conversion & Type Promotion
↓
Operators
↓
Control Statements
↓
Loops
↓
OOPNow that we know how Java can make decisions, repeat operations, and change the flow of execution, we’re ready to learn MOST important building block of Java:
OOPS
We’ll understand what is OOPS and other concepts in oops. As we cannot learn the entire oops concept in one blog i will be breaking them into multiple blogs
One concept at a time. Keep learning, keep coding. 🚀





