Download - Control structures in Scala
![Page 1: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/1.jpg)
Control structures in Scala
CS Scala course 2012
![Page 2: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/2.jpg)
Overview: control structures
• if • while • for • match • try • Function calls
+ Exercise
![Page 3: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/3.jpg)
Main ideas
• Few built-in control structures – Others could be find in libraries
• They work much like imperative counterparts – But they also have values (most of them)
![Page 4: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/4.jpg)
Main ideas (2)
• In Java: – Expressions have values – Statements carries out an action
• In Scala: – In Scala, almost ALL constructs have values
![Page 5: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/5.jpg)
if
Can be written in Java style: if (n > 0) { r = r * n; n -= 1; }
![Page 6: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/6.jpg)
if (2)
var s = 0 if (x > 0) s = 1 else s = -1 val s = if (x > 0) 1 else -1
• Better because of val
• Semicolon optional
But if returns a value!
![Page 7: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/7.jpg)
if (3)
if statements must have some value => omitted else returns Unit (≈ Java void) if (x > 0) 1
equivalent to
if (x > 0) 1 else ()
Unit
![Page 8: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/8.jpg)
while
Loops can be written in Java style while (n > 0) { r = r * n; n -= 1; } There is also the do ... while loop
![Page 9: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/9.jpg)
while (2)
There is no: • break • continue Loops do NOT return a value => not used as often in Scala as in Java.
![Page 10: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/10.jpg)
for
Scala has no direct analog of the Java for for (initialize; test; update)
for (i <- 1 to n)
the to method returns a Range (also: until)
for (i <- expr) <- to traverse all values of the right expression
![Page 11: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/11.jpg)
for (2)
guard: an if inside the for Example: Filter out all numbers larger than 5
for (i <- expr; if i > 5 )
![Page 12: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/12.jpg)
for (3)
for (...) yield • Creates a new collection of the same type as
the original • Contains the expressions after the yield, one
for each iteration of the loop.
Example: Double all elements larger than 5 val doubles = for (i <- expr; if i > 5 ) yield 2 * i
![Page 13: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/13.jpg)
match
• Similar to switch statements • Returns a value • _ is used for default
val output = x match { case 1 => "one" case 2 => "two" case _ => "many" }
![Page 14: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/14.jpg)
try
• Exceptions work as in Java – But you don’t need to declare that a function might
throw an exception
try { process(new FileReader(filename)) } catch { case _: FileNotFoundException => println(filename + " not found") case ex: IOException => ex.printStackTrace() } finally { … }
finally
![Page 15: Control structures in Scala](https://reader036.vdocument.in/reader036/viewer/2022071602/613d52fd736caf36b75bfa0f/html5/thumbnails/15.jpg)
Summary
• An if expression has a value • A block has a value—the value of its last expression • The Scala for loop is like an “enhanced” Java for loop • Semicolons are (mostly) optional • The void type is Unit • Avoid using return in a function • Exceptions work just like in Java or C++, but you use a “pattern matching” syntax for catch.