This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util.control.Breaks._ | |
println("\n=== BREAK EXAMPLE ===") | |
breakable { | |
for (i <- 1 to 10) { | |
println(i) | |
if (i > 4) break // break out of the for loop | |
} | |
} | |
println("\n=== CONTINUE EXAMPLE ===") | |
val searchMe = "peter piper picked a peck of pickled peppers" | |
var numPs = 0 | |
for (i <- 0 until searchMe.length) { | |
breakable { | |
if (searchMe.charAt(i) != 'p') { | |
break // break out of the 'breakable', continue the outside loop | |
} else { | |
numPs += 1 | |
} | |
} | |
} | |
println("Found " + numPs + " p's in the string.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment