Scala: How to use break and continue in for and while loops

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.5, “ Scala: How to use break and continue in for loops (and while loops)”


You have a situation where you need to use a break or continue construct, but Scala doesn’t have break or continue keywords.

Scala ‘break’ and ‘continue’ examples

Scala FAQ: Can you share some examples of how to implement break and continue functionality in Scala?

Sure. The example Scala code below shows both a break and a continue example. As you can see from the import statement, it uses the code in the Scala util.control.Breaks package.

To understand how this works, let's first look at the code, and then the output. First, here's the code:

How to break long lines using AppleScript

AppleScript FAQ: How do I break a long line in AppleScript?

When you program in AppleScript, you can very easily start creating programs with very long lines, which are hard to read. And AppleScript doesn't like it when you use carriage returns to break your long lines, which is a bit of a bummer.

But, fear not, if you need to break a long line in AppleScript, you just need to know the magic keystroke character: Just type the [Option][Enter] or [Option][L] characters when you want to break a long line.

Perl loop - how to break out of a loop in Perl

Perl loop break FAQ: How do I break out of a Perl loop?

Problem: You're writing some Perl loop code (for, foreach, or while), and you have a condition where you need to break out of your loop early, and you quickly find out that Perl doesn't have a 'break' operator.

The Perl "break" statement

In many programming languages you use the break operator to break out of a loop like this, but in Perl you use the last operator to break out of a loop, like this:

Java split String: How to split a String into tokens with StringTokenizer

Java String FAQ: How do I split a String into substrings?

To split a string in Java into substrings, use the splitmethod of the String class. For instance, given the following string:

String speech = "Four score and seven years ago";

You can split the string into substrings using the following line of code:

LaTeX line break - How to prevent line breaks in LaTeX documents

LaTeX FAQ: How can I prevent line breaks from occurring in my LaTeX documents?

Another nice thing about LaTeX is that you can prevent line breaks between words that should not be broken, things like people's names. To prevent a line break from occurring, use the tilde character (~) instead of a space, like this:


This keeps "Mr. John Doe" on the same line when your document is created.