Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 779421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:55:32+00:00 2026-05-14T19:55:32+00:00

How do I break out a loop? var largest=0 for(i<-999 to 1 by -1)

  • 0

How do I break out a loop?

var largest=0
for(i<-999 to 1 by -1) {
    for (j<-i to 1 by -1) {
        val product=i*j
        if (largest>product)
            // I want to break out here
        else
           if(product.toString.equals(product.toString.reverse))
              largest=largest max product
    }
}

How do I turn nested for loops into tail recursion?

From Scala Talk at FOSDEM 2009 http://www.slideshare.net/Odersky/fosdem-2009-1013261
on the 22nd page:

Break and continue
Scala does not have them. Why?
They are a bit imperative; better use many smaller functions
Issue how to interact with closures.
They are not needed!

What is the explanation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:55:33+00:00Added an answer on May 14, 2026 at 7:55 pm

    You have three (or so) options to break out of loops.

    Suppose you want to sum numbers until the total is greater than 1000. You try

    var sum = 0
    for (i <- 0 to 1000) sum += i
    

    except you want to stop when (sum > 1000).

    What to do? There are several options.

    (1a) Use some construct that includes a conditional that you test.

    var sum = 0
    (0 to 1000).iterator.takeWhile(_ => sum < 1000).foreach(i => sum+=i)
    

    (warning–this depends on details of how the takeWhile test and the foreach are interleaved during evaluation, and probably shouldn’t be used in practice!).

    (1b) Use tail recursion instead of a for loop, taking advantage of how easy it is to write a new method in Scala:

    var sum = 0
    def addTo(i: Int, max: Int) {
      sum += i; if (sum < max) addTo(i+1,max)
    }
    addTo(0,1000)
    

    (1c) Fall back to using a while loop

    var sum = 0
    var i = 0
    while (i <= 1000 && sum <= 1000) { sum += i; i += 1 }
    

    (2) In Scala 3.3+, use boundaries instead (for more info):

    import scala.util.boundary, boundary.break
    boundary {
      for i <- 0 to 1000 do
        sum += i
        if sum >= 1000 then break()
    }
    

    (3) Throw an exception.

    object AllDone extends Exception { }
    var sum = 0
    try {
      for (i <- 0 to 1000) { sum += i; if (sum>=1000) throw AllDone }
    } catch {
      case AllDone =>
    }
    

    (3a) In Scala 2.8+ this is already pre-packaged in scala.util.control.Breaks using syntax that looks a lot like your familiar old break from C/Java:

    import scala.util.control.Breaks._
    var sum = 0
    breakable { for (i <- 0 to 1000) {
      sum += i
      if (sum >= 1000) break
    } }
    

    (4) Put the code into a method and use return.

    var sum = 0
    def findSum { for (i <- 0 to 1000) { sum += i; if (sum>=1000) return } }
    findSum
    

    This is intentionally made not-too-easy for at least three reasons I can think of. First, in large code blocks, it’s easy to overlook "continue" and "break" statements, or to think you’re breaking out of more or less than you really are, or to need to break two loops which you can’t do easily anyway–so the standard usage, while handy, has its problems, and thus you should try to structure your code a different way. Second, Scala has all sorts of nestings that you probably don’t even notice, so if you could break out of things, you’d probably be surprised by where the code flow ended up (especially with closures). Third, most of Scala’s "loops" aren’t actually normal loops–they’re method calls that have their own loop, or they are recursion which may or may not actually be a loop–and although they act looplike, it’s hard to come up with a consistent way to know what "break" and the like should do. So, to be consistent, the wiser thing to do is not to have a "break" at all.

    Note: There are functional equivalents of all of these where you return the value of sum rather than mutate it in place. These are more idiomatic Scala. However, the logic remains the same. (return becomes return x, etc.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how to break out of a for loop when it's typed
What if I have nested loops, and I want to break out of all
Possible Duplicate: Nested jQuery.each() - continue/break Here is my code: var steps = $(div#f);
In a normal loop you can break out of a loop using break. Can
I'm trying to break out of a loop if a certain condition is true.
I have a simple C# foreach loop, how can I break out of the
How do you break out of a foreach loop while within a switch block?
Suppose I need to break out of three or four nested for loops at
I'd like to break out of the currently running program and be dropped back
I'm really trying to break out of my RDBMS mind set when setting up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.