if

When using if for process control, it includes single branch, double branch, and multiple branches.
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 expressions have return values, so he can also do this:

  • If the type of the variable is not specified, the compiler will automatically infer
  • If you do not write else, it will default to else (), so x1 prints ()
  • 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 of for in Scala is: for (i <- expression/array/set), i is the variable of the loop
For example, output 0 to 5, written like this in java:

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

This is how it is written in Scala. The difference between to and until is that to includes the following number, and until does not include the following number.

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

For example, to output the information of the array, java is written 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 is 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 outputting 0 to 5, 3 is not output. Java writes like this:

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

Scala is written like this:

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

It can also be written like this, and the conditions are written in the for:

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

To print a two-dimensional array, java is written like this:

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

Scala is written like this:

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

while

The usage of while is basically the same as that of java. For example, print statements 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 or out of the current loop for for or while. Scala uses the higher-order function breakable to do it.
For example, output 0 to 4, and jump out of the loop when it reaches 3.
Java is written like this:

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

Scala is written like this:

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

For example, output 0 to 4, and jump out of the current loop when it reaches 3.
Java is written like this:

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

Scala is written like this:

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

大军
847 声望183 粉丝

学而不思则罔,思而不学则殆


引用和评论

1 篇内容引用
0 条评论