Control Statements in Java

Part 1: Decision-Making Statements

Decision-making statements execute specific blocks of code based on whether a condition evaluates to true or false.

Decision-Making flow in java

 

1. if, if-else, and nested if

A. The if Statement

The simplest decision-making statement. Executes a code block only if the condition is true.

int userAge = 20;

if (userAge >= 18) {
System.out.println("Access Granted: You are eligible to vote!");
}

 

B. The if-else Statement

Provides an alternative execution block when the condition evaluates to false.

int batteryPercentage = 15;

if (batteryPercentage > 20) {
System.out.println("Battery status: OK");
} else {
System.out.println("Warning: Low battery! Plug in your charger.");
}

 

C. The if-else if-else Ladder

Used when testing multiple conditions in sequence.

int score = 85;

if (score >= 90) {
System.out.println("Grade: A+");
} else if (score >= 80) {
System.out.println("Grade: A");
} else if (score >= 70) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: Needs Improvement");
}

 

D. nested if Statements

An if statement placed inside another if statement. Used when a secondary condition must be checked only after a primary condition is satisfied.

boolean hasAccount = true;
boolean correctPassword = true;

if (hasAccount) {
if (correctPassword) {
System.out.println("Login successful! Welcome back.");
} else {
System.out.println("Incorrect password. Try again.");
}
} else {
System.out.println("Account not found. Please sign up.");
}

2. The switch Statement

The switch statement selects one of many code blocks to execute based on the exact match of a single variable or expression. It serves as a cleaner, faster alternative to multiple if-else statements when comparing discrete values.

Important Keywords:

  • case: Defines a value to test against the switch expression.

  • break: Exits the switch block once a matching case is executed (prevents fall-through).

  • default: Executes if no matching case is found (similar to an else block).

 

int dayOfWeek = 3;

switch (dayOfWeek) {
case 1:
System.out.println("Monday - Start of the week!");
break;
case 2:
System.out.println("Tuesday - Keep grinding!");
break;
case 3:
System.out.println("Wednesday - Mid-week hustle!");
break;
case 4:
System.out.println("Thursday - Almost there!");
break;
case 5:
System.out.println("Friday - Weekend ready!");
break;
default:
System.out.println("Weekend mode active!");
break;
}

Part 2: Looping Statements (Iteration)

Loops allow you to repeat a block of code multiple times without rewriting it.

3. The for Loop

The for loop is ideal when you know in advance exactly how many times you want to execute a block of code.

Syntax Breakdown:

for (initialization; condition; update) {
// Code block to be executed
}
  • Initialization: Sets a counter variable (executed once).

  • Condition: Evaluates before every iteration. If true, the loop runs.

  • Update: Increments or decrements the counter after each loop iteration.

 

// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration number: " + i);
}

 

4. The while Loop

The while loop repeats a code block as long as a specified condition remains true. Use it when the exact number of iterations is not known in advance.

int count = 1;

while (count <= 3) {
System.out.println("Loading resources... Step " + count);
count++; // Increment counter to avoid an infinite loop
}

 

5. The do-while Loop

The do-while loop is an exit-controlled loop. It guarantees that the code block executes at least once because the condition is checked after the execution of the block.

int userChoice = 0;

do {
System.out.println("--- Menu ---");
System.out.println("1. Start Game");
System.out.println("2. Exit");
// Code executes at least once even if choice equals 2
} while (userChoice != 0 && userChoice < 2);

 

Part 3: Jump Statements

Jump statements alter the normal execution flow of loops.

 

6. break and continue

A. The break Statement

Instantly terminates the nearest loop or switch block and jumps execution to the code immediately following the loop.

// Search for a specific number and stop looping when found
for (int i = 1; i <= 10; i++) {
if (i == 5) {
System.out.println("Target number 5 found! Stopping loop.");
break; // Exits loop completely
}
System.out.println("Searching index: " + i);
}

 

B. The continue Statement

Skips the current iteration of a loop and moves directly to the next iteration.

// Print only odd numbers from 1 to 6
for (int i = 1; i <= 6; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number: " + i);
}

Java Loops Summary comparison table