Break and continue behavior
📖 Scenario: You are organizing a small event and have a list of guests with their ages. You want to process this list to find guests who are adults and stop checking once you find a guest who is exactly 21 years old. You also want to skip any guests who are under 18.
🎯 Goal: Build a Kotlin program that uses break and continue statements inside a for loop to process a list of guest ages. The program should skip guests under 18, stop processing when a guest aged 21 is found, and print the ages of guests processed.
📋 What You'll Learn
Create a list of guest ages called
guestAges with the exact values: 16, 17, 18, 19, 20, 21, 22Create a variable called
processedAges as an empty mutable list of integersUse a
for loop with variable age to iterate over guestAgesInside the loop, use
continue to skip ages less than 18Inside the loop, use
break to stop the loop when age equals 21Add each processed age to
processedAgesPrint the
processedAges list at the end💡 Why This Matters
🌍 Real World
This project shows how to control loops when processing lists, which is common when filtering or searching data like guest lists, orders, or sensor readings.
💼 Career
Understanding break and continue helps in writing efficient code that can skip unnecessary work or stop early, important skills for software developers and data analysts.
Progress0 / 4 steps