Scala IF…ELSE statement


Release date:2023-11-14 Update date:2023-11-14 Editor:admin View counts:160

Label:

Scala IF…ELSE statement

A Scala IF…ELSE statement is a block of code that determines execution by the execution result of one or more statements (True or False).

You can use the following figure to briefly understand the execution of conditional statements:

Image0

If statement

if statements consist of Boolean expressions and subsequent statement blocks.

Grammar

The if syntax format of the statement is as follows:

If (Boolean expression)
{
//If the Boolean expression is true, execute the statement block
}

If the Boolean expression is true executes the statement block within the curly braces, otherwise skips the statement block within the curly braces and executes the statement block after the curly braces.

Example

Example

object Test {
   def main(args: Array[String]) {
      var x = 10;
      if( x < 20 ){
         println("x < 20");
      }
   }
}

Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
x < 20

If…else statement

if statement can be followed by else statement else block of statements within can be specified in the Boolean expression as false will be carried out when.

Grammar

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

If (Boolean expression){
//If the Boolean expression is true, execute the statement block
}Else{
//If the Boolean expression is false, execute the statement block
}

Example

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, and the output is as follows:

$ scalac Test.scala
$ scala Test
x > 20

If…else if…else statement

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

Grammar

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

If (Boolean expression 1){
//If Boolean expression 1 is true, execute the statement block
}Else if (Boolean expression 2){
//If Boolean expression 2 is true, execute the statement block
}Else if (Boolean expression 3){
//If Boolean expression 3 is true, execute the statement block
}Else{
//If all the above conditions are false, execute the statement block
}

Example

Example

Object Test{
Def main (args: Array [String]){
Var x=30;
If (x==10){
Println ("X has a value of 10");
}Else if (x==20){
Println ("X has a value of 20");
}Else if (x==30){
Println ("X has a value of 30");
}Else{
Println ("Unable to determine the value of X");
}
}
}

Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
The value of X is 30

If…else nested statement

if...else nested statements can be implemented in the if statement to embed one or more if statement.

Grammar

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

If (Boolean expression 1){
//If Boolean expression 1 is true, execute the statement block
If (Boolean expression 2){
//If Boolean expression 2 is true, execute the statement block
}
}

else is similar to the nested statements of if...else nested statements.

Example

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, and the output is as follows:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).