[Scala process control] 08. Select structure

insert image description here

1. If/else statement

In Scala, flow control statements if/elseare 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/elseThe basic syntax of the statement is as follows:

if (condition) {
    
    
  // 如果条件为真,则执行这里的代码块
} else {
    
    
  // 如果条件为假,则执行这里的代码块
}

in:

  • conditionis a Boolean expression that executes the code block if its result trueis , otherwise executes the code block (if it exists).ifelse
  • ifThe 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 xthe value of the variable, if it xis greater than 5, the execution ifcode block outputs "x is greater than 5", otherwise the execution elsecode block outputs "x is less than or equal to 5".

Two, multi-branch

In Scala, if/elsestatements 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, scoredifferent ratings are output according to different values ​​of the score.

Three, if/else expression

It should be noted that, in Scala, if/elseis an expression that can return a value. Therefore, if ifthe code block and elsethe code block return values ​​of different types, if/elsethe resulting type of the entire expression will be inferred to be their common parent type. This property makes if/elsestatements very useful in functional programming.

val score = 80
val result = if (score >= 60) "及格" else "不及格" // 作为表达式,值赋给变量result

In Scala, if/elsea statement is an expression (Expression), which can return a value. Therefore, you can if/elseassign 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/elsean 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 xthe value of the variable, which is assigned to the variable . Depending on the value of the variable will hold a different string.if/elseresultxresult

If if/elsetwo 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, ifthe code block returns Stringa value of type , and elsethe code block returns Inta value of type , but because of Anytheir common parent type, resultthe type of the variable is inferred Any.

Note that although it is possible to if/elseassign 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.

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/132158708