Understanding Java Control Statements: A Beginner Friendly Guide
Introduction
If you are new to Java or just beginning your programming journey, this article is designed to help you understand Java Control Statements (Flow Control) from the ground up.
Control statements are essential because they define how a program executes, which code runs and how often it runs. This guide explains each concept step by step with clear explanations and practical Java examples.
What Are Control Statements?
Control statements control the flow of execution in a Java program.
They help decide:
Which block of code should execute
How many times a block should repeat
When execution should stop or skip
Real-Life Example
If it rains, take an umbrella.
Otherwise, go without it.
Java uses the same logic through control statements.
1. Decision-Making Statements
Decision-making statements allow a program to choose between different paths based on conditions.
1.1 if Statement
The if statement executes code only when a condition is true.
Syntax
if (condition) {
// code to execute
}
Example
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote");
}
Explanation
The condition evaluates to true
The message is printed
If the condition is false, nothing happens
1.2 if-else Statement
Used when there are two possible outcomes.
Syntax
if (condition) {
// true block
} else {
// false block
}
Example
int marks = 45;
if (marks >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
1.3 else-if Ladder
Used to check multiple conditions in sequence.
Example
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
Key Point
Only the first true condition is executed.
1.4 switch Statement
Used when comparing a single variable against fixed values.
Example
int day = 3;
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");
}
Note
The break statement prevents fall-through to the next case.
2. Looping Statements
Loops allow a block of code to execute repeatedly.
2.1 for Loop
Used when the number of iterations is known.
Example
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
2.2 while Loop
Executes as long as the condition remains true.
Example
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
2.3 do-while Loop
Executes the loop body at least once, even if the condition is false.
Example
int i = 10;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
2.4 Enhanced for Loop (For-Each)
Best used for arrays and collections.
Example
int[] numbers = {10, 20, 30};
for (int n : numbers) {
System.out.println(n);
}
3. Number Series Programs (Very Important)
Number series questions are commonly asked in interviews, exams and coding tests.
3.1 Print Numbers from 1 to 10
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
3.2 Print Even Numbers from 1 to 20
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
3.3 Print Odd Numbers from 1 to 20
for (int i = 1; i <= 20; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
3.4 Print Reverse Number Series (10 to 1)
for (int i = 10; i >= 1; i--) {
System.out.print(i + " ");
}
3.5 Sum of Numbers from 1 to 5
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
System.out.println("Sum = " + sum);
4. Jump Statements
Jump statements change the normal flow of execution.
4.1 break
Stops loop execution immediately.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
4.2 continue
Skips the current iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
4.3 return
Exits a method and returns a value.
static int add(int a, int b) {
return a + b;
}
Quick Interview Revision Table
| Statement | Purpose |
| if | Single condition |
| else-if | Multiple conditions |
| switch | Fixed values |
| for | Known count |
| while | Unknown count |
| do-while | Executes at least once |
| break | Stops loop |
| continue | Skips iteration |
| return | Exits method |
Conclusion
Java Control Statements form the foundation of logical programming.
A strong understanding of these concepts helps you write efficient programs and perform confidently in technical interviews.
If you want, next I can write: