Skip to main content

Command Palette

Search for a command to run...

Java Control Flow Statements:

Published
•3 min read•View as Markdown
Java Control Flow Statements:
L

Hi, I’m Lilavati Mhaske — a tech enthusiast, Java learner, and aspiring full stack developer. I created this blog to document and share my journey through Full Stack Java development, from the fundamentals to frameworks like Spring Boot, REST APIs, databases, and frontend integration. I believe in learning by doing—and explaining! Whether you're just starting out or brushing up your Java skills, I hope my posts make your learning smoother and more enjoyable. Let’s grow together, one line of code at a time. ☕💻

if-else, else-if, switch, and Loops

Control flow statements in Java determine how the program's execution flows based on conditions or repeated actions. In this post, we’ll explore conditional branching (if, else if, else, switch) and looping constructs (for, while, do-while) in Java, with detailed examples and code comments for better understanding.


Conditional Statements in Java

if Statement

The if statement executes a block only when the given condition evaluates to true.

javaCopyEditint number = 5;

// Check if the number is greater than 0
if (number > 0) {
    System.out.println("Positive number"); // This line will execute
}

if-else Statement

Use if-else to handle two possibilities.

javaCopyEditint number = -3;

// Check if the number is non-negative
if (number >= 0) {
    System.out.println("Positive or Zero");
} else {
    System.out.println("Negative number"); // This line will execute
}

else-if Ladder

The else-if ladder allows checking multiple conditions in sequence.

javaCopyEditint marks = 75;

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 75) {
    System.out.println("Grade B"); // This line will execute
} else if (marks >= 60) {
    System.out.println("Grade C");
} else {
    System.out.println("Grade D");
}

switch Statement

A cleaner alternative to multiple if-else conditions when comparing the same variable against multiple constant values.

javaCopyEditint day = 3;

switch (day) {
    case 1:
        System.out.println("Monday");
        break; // Exit switch after matching case
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday"); // This line will execute
        break;
    default:
        System.out.println("Invalid day"); // Executed if no case matches
        break;
}

Note: Without break, control falls through to the next case.


Looping Constructs in Java

for Loop

Use this when you know how many times to repeat.

javaCopyEdit// Print numbers from 0 to 4
for (int i = 0; i < 5; i++) {
    System.out.println("Iteration " + i); // Runs 5 times
}

while Loop

Use when the number of repetitions is unknown and depends on a condition.

javaCopyEditint i = 0;

// Loop while i is less than 5
while (i < 5) {
    System.out.println("While loop: " + i);
    i++; // Increment to avoid infinite loop
}

do-while Loop

Executes the loop body at least once, even if the condition is false initially.

javaCopyEditint i = 0;

do {
    System.out.println("Do-while loop: " + i); // Always executes once
    i++;
} while (i < 5);

Choosing the Right Loop

Loop TypeUse Case
forWhen iteration count is known beforehand
whileWhen condition-controlled repetition is needed
do-whileWhen loop must execute at least once

Common Mistakes and Tips

  • Uninitialized local variables in conditional blocks may cause compile-time errors.

  • Omitting the break in a switch can lead to fall-through behavior.

  • Ensure loop conditions eventually become false to avoid infinite loops.

  • Keep nested blocks readable using proper indentation and structure.


G

A good read

More from this blog

Full Stack Java Journey

5 posts

Exploring Java’s journey from its origins to modern Full Stack development. Follow along as I share insights, tutorials, and lessons from my Java learning experience. ☕💻