0
0
Javaprogramming~30 mins

Labeled break and continue in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Labeled break and continue in Java
📖 Scenario: Imagine you are managing a small warehouse with shelves and boxes. You want to find a specific box labeled "Target" among shelves and boxes. Sometimes you want to skip checking certain boxes or stop searching entirely when you find the target.
🎯 Goal: You will write a Java program that uses labeled break and labeled continue to control loops while searching for the "Target" box in a 2D array representing shelves and boxes.
📋 What You'll Learn
Create a 2D array called shelves with specific box labels.
Create a label called search for the outer loop.
Use labeled continue to skip boxes labeled "Skip".
Use labeled break to stop searching when the "Target" box is found.
Print the position of the "Target" box when found.
💡 Why This Matters
🌍 Real World
Labeled break and continue help manage complex nested loops, such as searching in grids, processing tables, or handling multi-level menus.
💼 Career
Understanding labeled break and continue is useful for writing clear and efficient code in Java, especially in jobs involving data processing, game development, or UI programming.
Progress0 / 4 steps
1
Create the shelves data structure
Create a 2D String array called shelves with these exact values: {{"Box1", "Box2", "Skip"}, {"Box3", "Target", "Box4"}, {"Skip", "Box5", "Box6"}}.
Java
Need a hint?

Use String[][] shelves = { ... }; with the exact rows and columns.

2
Add the labeled outer loop
Add a labeled outer for loop with label search to iterate over the rows of shelves using variable i.
Java
Need a hint?

Write search: before the outer for loop.

3
Add inner loop with labeled continue and break
Inside the search loop, add an inner for loop with variable j to iterate over shelves[i]. Use if to continue search when the box label is "Skip". Use if to break search when the box label is "Target". Print the found position with System.out.println("Found Target at shelf " + i + ", box " + j); before breaking.
Java
Need a hint?

Use nested loops and labeled continue and break with the label search.

4
Print the final output
Run the program to print the position of the "Target" box found. The output should be exactly: Found Target at shelf 1, box 1.
Java
Need a hint?

Make sure the print statement matches exactly and the program runs fully.