Pacific-Design.com

    
Home Index

1. Scala

2. 4 Loops Match

Scala / 4 Loops Match /

object LearningScala2 {

  // Flow control
  
  // If / else syntax
  if (1 > 3) println("Impossible!") else println("The world makes sense.")
                                                  //> The world makes sense.
  
  if (1 > 3) {
    println("Impossible!")
  } else {
    println("The world makes sense.")
  }                                               //> The world makes sense.
  
  // Matching - like switch in other languages:
  val number = 3                                  //> number  : Int = 3
  number match {
    case 1 => println("One")
    case 2 => println("Two")
    case 3 => println("Three")
    case _ => println("Something else")
    }                                         //> Three
    
    // For loops
    for (x <- 1 to 4) {
        val squared = x * x
        println(squared)
    }                                         //> 1
                                                  //| 4
                                                  //| 9
                                                  //| 16
                                                  
  // While loops
  var x = 10                                      //> x  : Int = 10
  while (x >= 0) {
    println(x)
    x -= 1
  }                                               //> 10
                                                  //| 9
                                                  //| 8
                                                  //| 7
                                                  //| 6
                                                  //| 5
                                                  //| 4
                                                  //| 3
                                                  //| 2
                                                  //| 1
                                                  //| 0
                                                  
  x = 0
  do { println(x); x+=1 } while (x <= 10)         //> 0
                                                  //| 1
                                                  //| 2
                                                  //| 3
                                                  //| 4
                                                  //| 5
                                                  //| 6
                                                  //| 7
                                                  //| 8
                                                  //| 9
                                                  //| 10
                                                  
   // Expressions
   // "Returns" the final value in a block automatically
   
   {val x = 10; x + 20}                           //> res0: Int = 30
                                                
     println({val x = 10; x + 20})            //> 30
         
       
}

Incremental Loop

object Loops {

    def main(args: Array[String]) {

        var i = 0
        var evenList = for { 
            
            i <- 1 to 10 
            if(i % 2) == 0

        } yield i

        for(i <- evenList) println(i)

    }
}

/*
Output

2
4
6
8
10
*/

/*Incremental Loop*/
object Loop2Increment {

    def main(args: Array[String]) {

        for( a <-1 to 3; b <- 4 to 6 ) {

            println("a=" + a + " b=" + b )

        }
    }
}

/*
Output

a=1 b=4
a=1 b=5
a=1 b=6
a=2 b=4
a=2 b=5
a=2 b=6
a=3 b=4
a=3 b=5
a=3 b=6
*/