Skip to content

Break & Continue ๐Ÿ›‘ Skip

Mentor's Note: A loop is a train on a track. But what if there's a problem? Break is like the Emergency Brake (Stops the train), and Continue is like a Small Jump (Skips one station and goes to the next). ๐Ÿ’ก


๐ŸŒŸ The Scenario: The Grocery Trip ๐Ÿ›’

Imagine you are at a supermarket with a list of 10 items to buy.

  • The Break (The "I'm Done" Brake): You have 10 items on your list, but by the 4th item, you realize you've Ran out of Money! ๐Ÿ’ฐ You Break (Stop) the whole trip and go directly to the exit. ๐Ÿšช
  • The Continue (The "Out-of-Stock" Skip): You want to buy 10 items, but the 3rd item (Bread) is Out of Stock. ๐Ÿšซ You Continue (Skip) the bread and immediately move to the next item (Milk) on your list. ๐Ÿงบ
  • The Result: You have total control over your journey through the loop. โœ…

๐ŸŽจ Visual Logic: Stop vs Skip

Statement Role Analogy Visual Action
break Exit the loop ๐Ÿ›‘ The Emergency Brake Entire Loop Stops!
continue Skip current turn โญ๏ธ The Small Jump Move to the Next Item!
graph TD
    A[Loop Start ๐Ÿ”„] --> B{Condition?}
    B -- Match? --> C{Break or Continue?}
    C -- Break ๐Ÿ›‘ --> D[EXIT LOOP ๐Ÿšช]
    C -- Continue โญ๏ธ --> E[NEXT TURN ๐Ÿ”]
    B -- No Match --> F[Run Code โš™๏ธ]
    F --> E

๐Ÿ“– Concept Explanation

1. The break Statement ๐Ÿšช

Immediately terminates the loop and moves to the first line of code AFTER the loop block.

2. The continue Statement โญ๏ธ

Stops the current "turn" (iteration) of the loop and jumps immediately back to the Condition Check to start the next turn.


๐Ÿ’ป Implementation: The Grocery Lab

// ๐Ÿ›’ Scenario: Grocery Shopping
// ๐Ÿš€ Action: Using break and continue to control the trip

public class Main {
    public static void main(String[] args) {
        System.out.println("--- Starting Trip ---");

        for (int item = 1; item <= 10; item++) {
            // โญ๏ธ 1. Continue: Out of Stock at Item #3
            if (item == 3) {
                System.out.println("Item #3 is Out of Stock. Skipping... ๐Ÿšซ");
                continue; 
            }

            // ๐Ÿ›‘ 2. Break: Out of Money at Item #6
            if (item == 6) {
                System.out.println("Ran out of Money! Ending trip... ๐Ÿ’ฐ");
                break;
            }

            System.out.println("Buying Item #" + item + " ๐Ÿงบ");
        }

        System.out.println("--- Exit Supermarket ---");
    }
}

๐Ÿ“Š Sample Dry Run (Logic)

Turn Item # if (3?) if (6?) Output Action
1 1 No No "Buying #1" Normal ๐Ÿงบ
2 2 No No "Buying #2" Normal ๐Ÿงบ
3 3 Yes โœ… - "Skipping #3" Skip โญ๏ธ
4 4 No No "Buying #4" Normal ๐Ÿงบ
5 6 No Yes โœ… "Ending trip" EXIT ๐Ÿšช

๐Ÿ“ˆ Technical Analysis: The while Loop Trap โš ๏ธ

Using continue in a while loop can be dangerous! - The Problem: If you put continue BEFORE your i++ (increment), your loop will jump back to the start and stay on the same number forever! - The Solution: Always make sure your counter updates before you skip. ๐Ÿ›ก๏ธ


๐ŸŽฏ Practice Lab ๐Ÿงช

Task: The Lucky Draw

Task: Loop from 1 to 20. Skip the number 13 (Unlucky) using continue. Stop the whole loop at 15 using break. Goal: Master the combination of skip and stop logic. ๐Ÿ’ก


๐Ÿ’ก Interview Tip ๐Ÿ‘”

"Interviewers often ask: 'Does break exit from all nested loops?' Answer: NO. A standard break only exits the innermost loop. To exit all of them, you need a Labeled Break (e.g., break myLabel;)."


๐Ÿ’ก Pro Tip: "Use continue to make your code cleaner. Instead of wrapping everything in a giant 'if', just skip the items you don't want at the top!" - Vishnu Damwala


โ† Back: Loops | Next: Arrays โ†’