Article directory
1. If/else statement
In Scala, flow control statements if/else
are used to execute different blocks of code based on conditions. It is similar to statements in other programming languages if/else
and decides which block of code to execute based on a given condition. if/else
The basic syntax of the statement is as follows:
if (condition) {
// 如果条件为真,则执行这里的代码块
} else {
// 如果条件为假,则执行这里的代码块
}
in:
condition
is a Boolean expression that executes the code block if its resulttrue
is , otherwise executes the code block (if it exists).if
else
if
The following condition can be any expression that returns a Boolean value, such as comparison, logical operation, function call, etc.
Example:
val x = 10
if (x > 5) {
println("x is greater than 5")
} else {
println("x is less than or equal to 5")
}
In the above example, according to x
the value of the variable, if it x
is greater than 5, the execution if
code block outputs "x is greater than 5", otherwise the execution else
code block outputs "x is less than or equal to 5".
Two, multi-branch
In Scala, if/else
statements can also implement multi-branching:
val score = 75
if (score >= 90) {
println("Excellent")
} else if (score >= 80) {
println("Good")
} else if (score >= 70) {
println("Average")
} else {
println("Fail")
}
In the above example, score
different ratings are output according to different values of the score.
Three, if/else expression
It should be noted that, in Scala, if/else
is an expression that can return a value. Therefore, if if
the code block and else
the code block return values of different types, if/else
the resulting type of the entire expression will be inferred to be their common parent type. This property makes if/else
statements very useful in functional programming.
val score = 80
val result = if (score >= 60) "及格" else "不及格" // 作为表达式,值赋给变量result
In Scala, if/else
a statement is an expression (Expression), which can return a value. Therefore, you can if/else
assign the result of to a variable. The variable's type is inferred from the return value type of the conditional block, or you can specify the variable's type explicitly.
Here is if/else
an example of assigning the result of to a variable:
val x = 10
val result = if (x > 5) {
"x is greater than 5"
} else {
"x is less than or equal to 5"
}
println(result) // 输出:"x is greater than 5"
In the above example, the expression returns different results depending on x
the value of the variable, which is assigned to the variable . Depending on the value of the variable will hold a different string.if/else
result
x
result
If if/else
two blocks of code return different types, Scala will infer the type of the variable based on their common supertype. For example:
val y = 10
val result: Any = if (y > 5) {
"x is greater than 5"
} else {
42
}
println(result) // 输出:"x is greater than 5"
In the above example, if
the code block returns String
a value of type , and else
the code block returns Int
a value of type , but because of Any
their common parent type, result
the type of the variable is inferred Any
.
Note that although it is possible to if/else
assign the result of a to a variable, in functional programming it is recommended to use immutable variables ( val
) and immutable data structures wherever possible to improve code safety and maintainability.