Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a do-until (postcondition) loop in Scala 2.8?

Tags:

loops

scala

An algorithm of mine could be better readable if I could use a postcondition (do-until) loop instead of precondition (while) loop. Is there such a feature in Scala 2.8?

like image 382
Ivan Avatar asked Oct 07 '10 00:10

Ivan


People also ask

What is DO WHILE loop in Scala?

Scala - do-while Loop. Unlike while loop, which tests the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop. A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time.

Why do we need more exact syntax in Scala?

Sometimes a program is clearer to read (or understand) with more exact syntax. Scala's syntax is flexible. We can omit, or add, parentheses in for-to and until loops. Ranges. We can directly loop over the numbers in a Range. The first argument to Range is the start, the second is the exclusive end.

What is the for-to and until syntax in Scala?

For to, until syntax. Sometimes a program is clearer to read (or understand) with more exact syntax. Scala's syntax is flexible. We can omit, or add, parentheses in for-to and until loops.

How to advance through a range of numbers in Scala?

We can use the "to" method to advance through a range of numbers. We must choose the best loop. For example. Let us begin with a for-loop that enumerates a list. We create a list with 3 strings in it. Then we use the for-each loop on the list. Tip The syntax for a for-each loop in Scala is similar to that for Java.


1 Answers

Sure.

scala> var i = 0
i: Int#4363 = 0

scala> do {
     | println(i)
     | i += 1
     | } while (i < 10)
0
1
2
3
4
5
6
7
8
9
res0: Unit#3773 = ()
like image 109
Daniel C. Sobral Avatar answered Nov 01 '22 21:11

Daniel C. Sobral