Scala only has a few built-in program control statements: if, while, for, try catch, and function calls. This is because function literals have been included since the birth of Scala, and the Scala kernel does not define too many control structures. Instead, the program's control structures can be extended with additional libraries.
1. if expression
Like other languages, if in Scala is also an option (branch), and unlike other languages, each expression can return a value.
var age = 25 val result = if(age > 20) "worker" else "student" println(result)
2. while loop
There are two forms, while(...){} and do{}while(...)
In Scala, while and do-while are called loops instead of expressions, because they have no return value, return Unit or ()
Three, for expression
1. A basic usage of for is similar to Java and can be used to enumerate collection elements
val filesHere = (new java.io.File(".")).listFiles for(file <- filesHere) println(file)
Supports all types of collection types
for(i <- 1 to 4) println(i)
2. Filter
A for expression can add a filter - add one or more if statements inside the for parentheses.
For example, instead of enumerating every element in a collection, iterate over only certain elements that qualify.
val filesHere = (new java.io.File(".")).listFiles for(file <- filesHere if file.isFile if file.getName.endsWith(".scala")) println(file)
3. Nested iteration
The for expression supports multiple iterations
4. Bind intermediate variables
5. Generate a new collection
Basic syntax: for clauses yield body , where clauses yield is the keyword
Fourth, use try expressions to handle exceptions
1. Throwing an exception
Exceptions are thrown in the same way as in Java
val half = if (n % 2 == 0) n/2 else throw new RuntimeException("n must be even")
2. Catch exceptions
try{}catch{}
3.finally
If there is a return value in finally, the return value in try will be overwritten, so try to avoid using return in finally
4. Generate return value
5. Match expression
This expression supports selecting one of multiple choices for execution, similar to switch in Java
true/false boolean expression match { case "salt" => println("pepper") case "chips" => println("salsa") case "eggs" => println("bacon") case _ => println("huh?") }
Pay attention to the following three points when using:
- One is that any type of constant can be used in a
case
statement, not just anint
enumeration type. - Second, each
case
statement does not need to be usedbreak
, Scala does not support "fall through
". - The third is the default matching of Scala
_
, its function is similar to that in javadefault
.
6. Scala does not provide break and continue, but some methods can be used to achieve the functions of break and continue
var i = 0 var foundIt=false while (i < args.length && !foundIt) { if (!args(i).startsWith("-")) { if(args(i).endsWith(".scala")) foundIt=true } i=i+1 }
Of course, Scala scala.util.control
defines break
control structures in packages. Its implementation is to call the function to the superior by throwing an exception.
import scale.util.control.Breaks._ import java.io._ val in = new BufferedReader(new InputStreamReader(System.in)) breakable { while(true) { println("? ") if(in.readLine()=="") break } }