Scala IF...ELSE statement


Scala IF...ELSE statement is a block of code that is executed based on the execution result (True or False) of one or more statements.

You can simply understand the execution process of conditional statements through the following figure:

if.png


if statement

if statement has a Boolean expression It consists of an expression and subsequent statement blocks.

Grammar

The syntax format of the if statement is as follows:

if(布尔表达式)
{
   // 如果布尔表达式为 true 则执行该语句块
}

If the Boolean expression is true, the statement block within the curly braces will be executed, otherwise the statement within the curly braces will be skipped block, execute the block of statements after the curly braces.

Example

object Test {
   def main(args: Array[String]) {
      var x = 10;

      if( x < 20 ){
         println("x < 20");
      }
   }
}

Run Example»

Execute the above code, the output result is:

$ scalac Test.scala 
$ scala Test
x < 20

if.. .else statement

The if statement can be followed by the else statement, and the statement block within the else can be executed when the Boolean expression is false.

Grammar

The syntax format of if...else is as follows:

if(布尔表达式){
   // 如果布尔表达式为 true 则执行该语句块
}else{
   // 如果布尔表达式为 false 则执行该语句块
}

Example

object Test {
   def main(args: Array[String]) {
      var x = 30;

      if( x < 20 ){
         println("x 小于 20");
      }else{
         println("x 大于 20");
      }
   }
}

Execute the above code, the output result is:

$ scalac Test.scala 
$ scala Test
x 大于 20

if...else if...else statement

The if statement can be followed immediately by the else if...else statement, which is useful in the case of multiple conditional judgment statements.

Grammar

if...else if...else The syntax format is as follows:

if(布尔表达式 1){
   // 如果布尔表达式 1 为 true 则执行该语句块
}else if(布尔表达式 2){
   // 如果布尔表达式 2 为 true 则执行该语句块
}else if(布尔表达式 3){
   // 如果布尔表达式 3 为 true 则执行该语句块
}else {
   // 如果以上条件都为 false 执行该语句块
}

Example

object Test {
   def main(args: Array[String]) {
      var x = 30;

      if( x == 10 ){
         println("X 的值为 10");
      }else if( x == 20 ){
         println("X 的值为 20");
      }else if( x == 30 ){
         println("X 的值为 30");
      }else{
         println("无法判断 X 的值");
      }
   }
}

Execute the above code, the output result is:

$ scalac Test.scala 
$ scala Test
X 的值为 30

if...else Nested statement

if...else Nested statement can embed one or more if statements within an if statement.

Grammar

if...else The syntax format of nested statements is as follows:

if(布尔表达式 1){
   // 如果布尔表达式 1 为 true 则执行该语句块
   if(布尔表达式 2){
      // 如果布尔表达式 2 为 true 则执行该语句块
   }
}

else If...else nested statements are similar to if...else nesting statement.

Example

object Test {
   def main(args: Array[String]) {
        var x = 30;
        var y = 10;

         if( x == 30 ){
            if( y == 10 ){
            println("X = 30 , Y = 10");
         }
      }
   }
}

Execute the above code, the output result is:

$ scalac Test.scala 
$ scala Test
X = 30 , Y = 10