The first thing to note is that in Scala 2.7 there is no break keyword. In Scala 2.8 there is a break control structure but it is slightly different than the Java break keyword. We will encounter that topic in a later lesson. The control structures I will quickly cover are: do-while, while, for and if.
For information about the Java case statement take a look at the several matching topics covered now and in the future.
Note: The Java ternary if statement does not exist in Scala instead the standard if statement is to be used. It is slightly more verbose but returns a value in the same way as a ternary if statement.
- scala> var i = 0;
- i: Int = 0
- scala> while( i<3 ){
- | println( i )
- | i += 1
- | }
- 0
- 1
- 2
- scala> i = 0
- i: Int = 0
- scala> do {
- | println( i )
- | i += 1
- | } while (i<3)
- 0
- 1
- 2
- scala> for(j <- 0 until 3) println (j)
- 0
- 1
- 2
- scala> if (i<3)
- more
- scala> val result = if (i<3)
- result: Int = 10
- scala> println (result)
- 10
- scala> if (i>10) println(1)
- scala> if (i<10)
- 1
- // Note that the return value is (). You can only get a meaningful return value if there is an else-clause.
- scala> val r = if (i<10)>
- r: Unit = ()
- scala> println(r)
- ()
No comments:
Post a Comment