An example of using ‘break’ in a Scala for loop

Here’s a little example of how to use a break in a Scala for loop:

import util.control.Breaks._

// other code here ...

var count = 0
var page = 1
breakable {
    for (entry <- entries) {
        if (count == 0 || count % 5 == 0) {
            println(s"\n(($feedName - $page))")
            println("----------------\n")
            page += 1
        }
        println(replaceBadCharacters(entry.getTitle))
        println("")
        count += 1
        // print a maximum of 20 headlines
        if (count == 20) break
    }
}

For information about how break and breakable work, see my tutorial, How to use break and continue in Scala for loops and while loops.