Control statements and loops allow a program to make decisions and execute a block of code repeatedly. They are essential for building interactive applications, validating user input, processing data, and implementing business logic.
In this article, you'll learn how to use decision-making statements and different types of loops in Java with practical examples.
Learning Objectives
Use if, if-else, and else-if statements
Work with nested if statements
Use the switch statement
Understand the break and continue statements
Write programs using for, while, and do-while loops
Use nested loops
Apply the enhanced for loop to iterate through arrays
Decision-Making Statements
Decision-making statements allow your program to execute different blocks of code based on specified conditions.
The if Statement
The if statement executes a block of code only if the specified condition is true.
Syntax
if (condition) {
// code to execute
}
Example
public class IfExample {
public static void main(String[] args) {
int marks = 75;
if (marks >= 50) {
System.out.println("Pass");
}
}
}
Output
Pass
The if-else Statement
Use if-else when there are two possible outcomes.
Example
public class IfElseExample {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("Eligible to Vote");
} else {
System.out.println("Not Eligible");
}
}
}
Output
Not Eligible
The else-if Ladder
The else-if ladder checks multiple conditions one after another.
Example
public class GradeExample {
public static void main(String[] args) {
int marks = 82;
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");
}
}
}
Output
Grade B
Nested if Statement
A nested if statement places one if statement inside another.
Example
public class NestedIfExample {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
if (age >= 18) {
if (hasLicense) {
System.out.println("Eligible to Drive");
}
}
}
}
Output
Eligible to Drive
The switch Statement
The switch statement is used when a variable can have multiple predefined values.
Syntax
switch (expression) {
case value:
// code
break;
default:
// code
}
Example
public class SwitchExample {
public static void main(String[] args) {
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");
}
}
}
Output
Wednesday
break Statement
The break statement immediately terminates a loop or switch statement.
Example
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break;
}
System.out.println(i);
}
Output
1
2
3
4
5
continue Statement
The continue statement skips the current iteration and moves to the next iteration of the loop.
Example
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output
1
2
4
5
Loops in Java
Loops allow a block of code to execute repeatedly until a condition becomes false.
Java provides three main looping statements:
for loop
while loop
do-while loop
for Loop
The for loop is used when the number of iterations is known.
Syntax
for (initialization; condition; increment) {
// code
}
Example
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
Output
1
2
3
4
5
while Loop
The while loop executes as long as the condition remains true.
Syntax
while (condition) {
// code
}
Example
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}
Output
1
2
3
4
5
do-while Loop
The do-while loop executes the code block at least once before checking the condition.
Syntax
do {
// code
} while (condition);
Example
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}
Output
1
2
3
4
5
Nested Loops
A nested loop is a loop inside another loop. It is commonly used to display patterns or process rows and columns.
Example
public class NestedLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output
* * *
* * *
* * *
Enhanced for Loop
The enhanced for loop simplifies iterating through arrays and collections.
Syntax
for (dataType variable : array) {
// code
}
Example
public class EnhancedForExample {
public static void main(String[] args) {
int[] marks = {80, 85, 90, 95};
for (int mark : marks) {
System.out.println(mark);
}
}
}
Output
80
85
90
95
Choosing the Right Loop
Loop | Best Used When |
for | Number of iterations is known |
while | Number of iterations is unknown and depends on a condition |
do-while | The code must execute at least once |
Enhanced for | Iterating through arrays or collections |
Common Mistakes
Avoid these common errors when using control statements and loops:
Forgetting curly braces {} for multiple statements.
Missing the break statement in a switch, causing unintended execution of subsequent cases.
Creating infinite loops by not updating the loop variable.
Using = instead of == in conditions.
Forgetting to increment or decrement the loop counter.
Practice Exercise
Write Java programs to:
Check whether a number is positive or negative using an if-else statement.
Display grades using an else-if ladder.
Print the day of the week using a switch statement.
Print numbers from 1 to 20 using a for loop.
Display even numbers between 1 and 50.
Print a multiplication table for a given number.
Display a star (*) pattern using nested loops.
Print all values from an integer array using an enhanced for loop.
Still need help?
Contact us