Friday 26 June 2015

Lesson 2 - while loops

While loop come in two flavors:
* while (condition) {}, and
* do {...} while (condition)

object WhileLoop {
  def main(args: Array[String]) {
    var x = 10;
    while (x > 5) {
      x = x - 1
      print(x + " ")
    }
    println
    
    do {
      x = x + 1
      print(x + " ")
    } while (x < 10)
    println
  }
}
The above code produces:
9 8 7 6 5 
6 7 8 9 10 

Scala does not support break statements as a langauge construct. See this post for a full list of break-like solutions.

No comments:

Post a Comment