Mastering Control Statements in Java: A Guide to Conditional Logic and Flow Control

Arpit Bhatt
4 min readNov 3, 2024

--

Control statements in Java change the program's flow based on certain conditions or repeated execution. These statements enable conditional execution, looping, and early exits from loops or functions.

Here’s an overview of the main types:

1. Conditional Statements

  • if Statement: Executes a code block only if a specified condition is true.

1. if Statement

Executes a block of code only if a specified condition is true.

int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
  • if-else Statement: Adds an alternative block that runs if the condition is false.
int temperature = 25;
if (temperature > 30) {
System.out.println("It's a hot day.");
} else {
System.out.println("It's a cool day.");
}
  • else if Ladder: Chains multiple conditions together, executing the first true condition's block.
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
  • switch Statement: Selects and executes code from multiple options based on the value of an expression.de
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");
break;
}

2. Looping Statements

  • for Loop: Repeats a block of code a specific number of times, using a counter variable.
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
  • while Loop: Repeats a code block as long as a specified condition is true.
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}

do-while Loop: Executes a block of code at least once, then repeats it as long as a condition is true.

int number = 1;
do {
System.out.println("Number: " + number);
number++;
} while (number <= 5);

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

4. Enhanced for Loop (for-each loop)

Used for iterating over arrays or collections.

int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println("Number: " + num);
}

Output:

Number: 10
Number: 20
Number: 30
Number: 40
Number: 50

3. Jump Statements

  • break: Exits the loop or switch statement, ending its execution immediately.
  • Immediately exits the loop or switch statement.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exits the loop when i is 3
}
System.out.println("i = " + i);
}

Output:

i = 1
i = 2
  • continue: Skips the current iteration in a loop and moves to the next one.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips the rest of the code when i is 3
}
System.out.println("i = " + i);
}

Output:

i = 1
i = 2
i = 4
i = 5
  • return: Exits a method and optionally returns a value to the calling code.
public static int addNumbers(int a, int b) {
return a + b; // Returns the result of the addition
}

public static void main(String[] args) {
int result = addNumbers(5, 10);
System.out.println("Result: " + result);
}

Output:

Result: 15

Labeled Statements

  • Labeled statements are used with break or continue to exit or skip iterations in nested loops. The label identifies the specific loop to control.

1. break with a Label

Exits a specific loop in a nested loop structure.

outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break outerLoop; // Exits the outer loop when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}

Output:

i = 1, j = 1

Explanation: When j is 2, the break statement with the outerLoop label stops both loops.

2. continue with a Label

Skips the current iteration of a specific loop in a nested structure and proceeds with the next iteration.

outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop; // Skips to the next iteration of the outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}

Output:

i = 1, j = 1
i = 2, j = 1
i = 3, j = 1

Explanation: When j is 2, the continue statement with the outerLoop label skips to the next iteration of the outerLoop, ignoring the remaining inner loop iterations.

3. Multiple Nested Loops with Labeled break

A more complex example where labeled break is used to exit a deeply nested loop.

outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 3; k++) {
if (k == 2) {
break outerLoop; // Exits all loops when k is 2
}
System.out.println("i = " + i + ", j = " + j + ", k = " + k);
}
}
}

Output:

i = 1, j = 1, k = 1

Explanation: When k is 2, the labeled break exits all loops immediately.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

follow us on Instagram for more posts and programming tips.

https://www.instagram.com/programming_pulse/

--

--

Arpit Bhatt
Arpit Bhatt

No responses yet