Control Structure and Functions in Scala

Control Structure and Functions in Scala

ยท

3 min read

  1. Conditional Statements

     if x > 0 then 1 else -1
     val s = if x > 0 then 1 else -1
    
     var t = 0
     if x > 0 then t = 1 else -1
    
  2. Input and Output

    We can read a line of input from the console with the readLine method of the scala.io.StdIn class. To read a numeric, Boolean, or character value, use readInt, readByte , readShort , readLong , readFloat , readBoolean , or readChar .

     val name = StdIn.readLine("Your name: ")
     println(name)
    
  3. Loops

    1. While Loops

       val n = 5
       while n > 0 do
         println(s"Printed $n")
      
       val n = 5
       while n > 0 do
         println(s"Printed $n")
       end while
      
    2. For Loops

       for i <- 1 to 5 do 
         println(i)
      

      For loop follows: for i โ† expr do

      ๐Ÿ’ก
      There is no val or var before the variable in the for loop. The type of the variable is the element type of the collection. The scope of the loop variable extends until the end of the loop.
  4. More For Loops

    We can have multiple generators of the form variable <- expression.

     for
       i <- 1 to 3
       j <- 1 to 3
     do
       print(s"${10 * i + j} ")  // 11 12 13 21 22 23 31 32 33
    

    A guard is a Boolean condition preceded by if:

     for
       i <- 1 to 3
       j <- 1 to 3
       if i != j
     do
       print(s"${10 * i + j} ")  // 12 13 21 23 31 32
    

    We can have any number of definitions, introducing variables that can be used inside the loop:

     for
       i <- 1 to 3
       from = 4 - i
       j <- from to 3
     do
       print(s"${10 * i + j} ")  // 13 22 23 31 32 33
    
     // It is equivalent to 
     for (i <- 1 to 3) {
       val from = 4 - i
       for (j <- from to 3) {
         print(s"${10 * i + j} ")
       }
     }
    

    When the body of the for loop starts with yield, the loop constructs a collection of values, one for each iteration:

     val result = for i <- 1 to 10 yield i // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
     val res = for c <- "Hello" yield (c+1).toChar // Ifmmp
    
  5. Variable Arguments

    We can call the function with as many argument as we want but they have to be of the same type.

     def sum(args: Int*) = {
       var result = 0
       for arg <- args do result += arg
       result
     }
    
     println(sum(1, 4, 9, 16, 25)) // 55
     sum(1 to 5) // Error
     sum((1 to 5)*) // Consider 1 to 5 as an argument sequence
    
  6. Lazy Values

    When a val is declared as lazy, its initialization is deferred until it is accessed for the first time.

     lazy val name = "Diwash"
    
  7. Exception

    Throwing exception:

     throw IllegalArgumentException("X should be negative")
    

    TRY CATCH:

     val url = URL("https://www.diwashmainali.com.np/")
    
     try
       process(url)
     catch
       case ex: IndexOutOfBoundsException => println(ex)
       case _: MalformedURLException => println(s"Bad URL: $url")  // use _ for variable name when not needed
    
ย