Showing posts with label while. Show all posts
Showing posts with label while. Show all posts

Monday, August 31, 2009

Java vs Scala Control Structures

This topic is mainly for completeness. We will quickly cover the standard control structures you find in Java and see how they are the same or different in Scala.

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.
  1. scala>var i = 0;
  2. i: Int = 0
  3. scala>while( i<3 ){
  4.      | println( i )
  5.      | i += 1
  6.      | }
  7. 0
  8. 1
  9. 2
  10. scala> i = 0
  11. i: Int = 0
  12. scala> do {
  13.      | println( i )
  14.      | i += 1
  15.      | } while (i<3)
  16. 0
  17. 1
  18. 2
  19. scala>for(j <- 0 until 3) println (j)  
  20. 0
  21. 1
  22. 2
  23. scala>if (i<3)
  24. more
  25. scala>val result = if (i<3)
  26. result: Int = 10
  27. scala> println (result)
  28. 10
  29. scala>if (i>10) println(1)
  30. scala>if (i<10)
  31. 1
  32. // Note that the return value is ().  You can only get a meaningful return value if there is an else-clause.
  33. scala>val r = if (i<10)>
  34. r: Unit = ()
  35. scala> println(r)
  36. ()

Monday, August 17, 2009

Return values

As with most functional languages, most control structures ( if, for, try ) return values. The common java idiom:
  1. String name=null;
  2. if( xxx ) name="yyy";
  3. else name="zzz";

can be replaced by
  1. val name = if( xxx ) "yyy"; else"zzz";

The benefit (other than less boiler plate code) is that name can now be a val instead of a var.

Another other point about returns: The return keyword is not required when returning a value from methods or control structures. The last value is always the return value. This is why you will get an error if the last line in a method or control structure is an assignment.

Examples:
  1. scala>val name = if( 1==2 ) "Jesse"else"Mauricio"
  2. name: java.lang.String = Mauricio
  3. scala> println(name)
  4. Mauricio
  5. scala>val collection = for( i <- 1 to 100; if(i%20 == 3) ) yield i
  6. collection: Seq.Projection[Int] = RangeFM(3, 23, 43, 63, 83)
  7. scala> collection.foreach( i => print( i +" ") )
  8. 3 23 43 63 83
  9. scala>val someObj:AnyRef = "Hello"
  10. someObj: AnyRef = Hello
  11. scala>val choice = someObj match {
  12.      | case _:java.io.File => "File"
  13.      | case _:String => "String"
  14.      | case _ => "Dunno"
  15.      | }
  16. choice: java.lang.String = String
  17. scala>val result = try {
  18.      | "two".toInt
  19.      | }catch{
  20.      | case e:NumberFormatException => -1
  21.      | case _ => 0
  22.      | }
  23. result: Int = -1
  24. scala>var i=0
  25. i: Int = 0
  26. // while and do-while do not have return values
  27. scala>while( i<4 ){
  28.      | "22"
  29.      | i += 2
  30.      | }
  31. scala> println( if(i>0) "great"else"less" )
  32. great
  33. // code blocks return the last statement
  34. scala>val m = {
  35.      | val x = 1
  36.      | x + 2
  37.      | }
  38. m: Int = 3