Break Statement with Levels in PHP
📖 Scenario: Imagine you are managing a small library system. You want to check shelves and books to find a specific book. Once found, you want to stop checking further shelves and books immediately.
🎯 Goal: You will create nested loops to simulate shelves and books. Then, you will use the break statement with levels to stop the loops once the book is found.
📋 What You'll Learn
Create two nested
for loops: outer loop for shelves (1 to 3), inner loop for books (1 to 5).Create a variable
targetBook with the value 3 to represent the book to find.Use
break 2; to exit both loops when the target book is found.Print a message showing which shelf and book was found.
💡 Why This Matters
🌍 Real World
Nested loops and breaking out of them are common when searching through multi-level data like shelves and books, menus and submenus, or rows and columns.
💼 Career
Understanding how to control loops with break levels helps in writing efficient code for data processing, web scraping, and game development.
Progress0 / 4 steps