Lesson 5
Controlling Kotlin Loops with Break and Continue Keywords
Intro to 'break' and 'continue' Statements

Hello! Today, we'll delve into the break and continue commands in Kotlin's loop structures. Much like a stop or skip sign on a road, these commands control the execution flow within a loop. A break exits the entire loop, much like stopping our journey midway. Conversely, continue skips to the next loop iteration, akin to bypassing one road and taking the next on our journey. Are you ready to see these in action?

Understanding 'break' through an Example

Imagine sifting through books. Once you find the one you seek, you stop — that's the break keyword in action. Here's how one simulates that in Kotlin:

Kotlin
1val bookStack = listOf("Java Basics", "Python for Beginners", "Web Development", "Learn Kotlin the Easy Way", "Advanced Algorithms") 2 3for (book in bookStack) { // Assuming a list of books 4 println("Checking book: $book") // Print each book it checks 5 if (book == "Learn Kotlin the Easy Way") { // If the book we want is found 6 println("Found the book!") // Announce that we found the book 7 break // Stop the book checking loop 8 } 9} 10 11/* 12 * Output: 13 * Checking book: Java Basics 14 * Checking book: Python for Beginners 15 * Checking book: Web Development 16 * Checking book: Learn Kotlin the Easy Way 17 * Found the book! 18 */

The break keyword stops the loop, thus saving unnecessary operations.

Understanding 'continue' through an Example

Next, let's get acquainted with continue through a sorting scenario. Suppose you want to ignore some items but allow the loop to run. Here's a Kotlin example:

Kotlin
1val ballBox = listOf("blue", "red", "red", "blue", "blue", "red", "blue") 2 3for (ball in ballBox) { // Loop over each ball in the box 4 if (ball == "red") { // If we find a red ball 5 continue // Skip this ball and continue 6 } 7 // This will only print if ball is not red, thus 'continue' facilitates the skipping of red balls 8 println("Sorted a $ball ball") 9} 10 11/* 12 * Output: 13 * Sorted a blue ball 14 * Sorted a blue ball 15 * Sorted a blue ball 16 * Sorted a blue ball 17 */

continue bypasses the current iteration, resuming the loop with the next, unlike break which terminates the whole loop.

Avoiding Pitfalls With 'break' and 'continue'

Remember, break and continue work within loops. In nested loops, they affect their immediate loop. Therefore, it's crucial to verify your loop levels before applying these keywords.

Kotlin
1for (i in 1..3) { // Outer loop 2 for (j in 1..3) { // Inner loop 3 if (j == 2) { 4 println("Break at i=$i, j=$j - Exiting the inner loop") 5 break // Exits only the inner loop 6 } 7 println("Processing i=$i, j=$j") 8 } 9} 10 11/* 12 * Output: 13 * Processing i=1, j=1 14 * Break at i=1, j=2 - Exiting the inner loop 15 * Processing i=2, j=1 16 * Break at i=2, j=2 - Exiting the inner loop 17 * Processing i=3, j=1 18 * Break at i=3, j=2 - Exiting the inner loop 19 */
Comparing 'break' and 'continue'

While both break and continue are used for loop control, they function differently. When break finds a match, it halts the loop, but continue bypasses the match and moves on to the next iteration. To sum it up, break is like stopping at a red light, and continue is like skipping to the next green signal.

Lesson Summary and Announcement of Practice Exercises

Fantastic work! You've successfully navigated through break and continue. Now, it's time to apply what we've learned through some exercises. Hands-on practice strengthens both your skills and your understanding, which will help you master break and continue. Are you ready to continue our fascinating learning journey? Let's go!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.