if

When using IF for process control, it includes single branch, double branch, and multiple branch. For example, Java is written like this.

public static void main(String[] args) { int num = 10; if (num == 10) { System.out.println("equal 10"); } if (num > 5) { System.out.println("greater than 5"); } else { System.out.println("less than 5"); } if (num < 5) { System.out.println("less than 5"); } else if (num > 20) { System.out.println("greater than 20"); } else { System.out.println("greater than 5 and less than 20"); }}

Scala is similar:

def main(args: Array[String]): Unit = {
    val num = 10
    if (num == 10) println("equal 10")

    if (num > 5) println("greater than 5")
    else println("less than 5")

    if (num < 5) println("less than 5")
    else if (num > 20) println("greater than 20")
    else println("greater than 5 and less than 20")
}

But Scala’s expressions return a value, so he could have done something like this:

  • If the type of the variable is not specified, the compiler will infer it automatically
  • If you don’t have an else, it’s going to default to else (), so x1 is going to print ()
  • If it is a block expression, the return value is the last line
def main(args: Array[String]): Unit = { var num = 10 var x = if (num > 5) 5 println(x) // 5 var x1 = if (num > 20) 5 println(x1) //() var y = if (num >  5) { println("num") 5 } println(y) //5 var z = if (num > 20) 20 else 5 println(z) //5 }

for

The syntax for for in Scala is: for (I < -expression/array/collection), I is a variable that loops like 0 to 5. In Java it would be written like this:

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

In Scala, the difference between “to” and “until” is that “to” includes the following number and “until” does not.

for (i <- 0 to 4) println(i)
for (i <- 0 until 5) println(i)

For example, to print information about an array, Java would write like this:

String[] arr = new String[]{"a", "b", "c"};
int length = arr.length;
for (int i = 0; i < length ; i++) {
    System.out.println(arr[i]);
}
for (String str : arr) {
    System.out.println(str);
}

It’s written like this in Scala

val length = arr.length
for (i <- 0 to length - 1) println(arr(i))
for (i <- arr) println(i)

For example, when printing 0 to 5, instead of 3, Java would write like this:

for (int i = 0; i < 5; i++) {
    if (i != 3) {
        System.out.println(i);
    }
}

Here’s what Scala says:

for (i <- 0 to 4) { if (i ! = 3) println(i) }

You can also write it like this, putting the condition inside the for:

for (i <- 0 to 4 if i ! = 3) println(i)

To print a two-dimensional array, Java would write:

for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.println("i=" + i + ",j=" + j); }}

Here’s what Scala says:

for (i <- 0 until 2; j <- 0 until 2) println("i=" + i + ",j=" + j)

while

While is used in much the same way as it is in Java. For example, to print a statement from 0 to 5:

var num = 0
while (num < 5) {
  println(num)
  num += 1
}

interrupt

In Java, we use break and continue to break out of the loop for or while or to break out of the current loop. Scala does this using a higher-order function called breakable. For example, output 0 to 4, and when it reaches 3, it breaks out of the loop. Here’s what Java says:

public static void main(String[] args) { for (int i = 0; i < 5; i++) { if (i == 3) { break; } System.out.println(i); }}

Here’s what Scala says:

import util.control.Breaks._
def main(args: Array[String]): Unit = {
    breakable(
      for (i <- 0 until 5) {
        if (i == 3) {
          break()
        }
        println(i)
      }
    )
}

For example, print 0 through 4, and break out of the current loop when it reaches 3. Here’s what Java says:

public static void main(String[] args) { for (int i = 0; i < 5; i++) { if (i == 3) { continue; } System.out.println(i); }}

Here’s what Scala says:

import util.control.Breaks._
def main(args: Array[String]): Unit = {
    for (i <- 0 until 5) {
      breakable(
        if (i == 3) {
          break()
        }else
        println(i)
      )
    }
}