WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Scala - IF ELSE Statements


Scala IF...ELSE statements are blocks of code that are executed by the execution result (True or False) of one or more statements.

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

if statement

The

if statement consists of a Boolean expression followed by a block of statements.

Syntax

The syntax of the

if statement is as follows:

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

If the Boolean expression is true, the block of statements inside the braces is executed, otherwise the block of statements inside the braces is skipped and the block of statements after the braces is executed.

example

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

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

Execute the above code, the output is:

$ scalac Test.scala 
$ scala Test
x < 20

if...else statement

The

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

Syntax

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

if(Boolean expression){
   // If Boolean expression for true Then execute the statement block
}else{
   // If Boolean expression for false Then execute the statement block
}

example

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

      if( x < 20 ){
         println("xLess than20");
      }else{
         println("x more than the 20");
      }
   }
}

Execute the above code, the output is:

$ scalac Test.scala 
$ scala Test
x more than the 20

if...else if...else Statement

The

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

Syntax

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

if(Boolean expression 1){
   // If Boolean expression 1  for true Then execute the statement block
}else if(Boolean expression 2){
   // If Boolean expression 2  for true Then execute the statement block
}else if(Boolean expression 3){
   // If Boolean expression 3  for true Then execute the statement block
}else {
   // If the above conditions are both false, execute the statement block
}

example

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

      if( x == 10 ){
         println("X Value 10");
      }else if( x == 20 ){
         println("X Value 20");
      }else if( x == 30 ){
         println("X Value 30");
      }else{
         println("Unable to determine the value of X");
      }
   }
}

Execute the above code, the output is:

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

if...else nested statements

if...else Nested statements can be implemented by embedding one or more if statements within an if statement.

Syntax

The

if...else nested statement syntax is as follows:

if(Boolean expression 1){
   // If Boolean expression 1  for true Then execute the statement block
   if(Boolean expression 2){
      // If Boolean expression 2  for true Then execute the statement block
   }
}

else if...else nested statements are similar to if...else nested statements.

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 is:

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