English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Scala IF...ELSE statements decide which code block to execute based on the execution result of one or more statements (True or False).
The execution process of the conditional statement can be simply understood from the following diagram:
The if statement consists of a boolean expression and the subsequent statement block.
The syntax format of 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, then execute the statement block inside the curly braces, otherwise skip the statement block inside the curly braces and execute the statement block after the curly braces.
object Test { def main(args: Array[String]) { var x = 10; if( x < 20 ){ println("x < 20"); } } }
Execute the above code, the output will be:
$ scalac Test.scala $ scala Test x < 20
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.
The syntax format of if...else is as follows:
if( boolean expression){ // If the boolean expression is true, then execute the statement block } else { // If the boolean expression is false, then execute the statement block }
object Test { def main(args: Array[String]) { var x = 30; if( x < 20 ){ println("x is less than 20"); } else { println("x is greater than 20"); } } }
Execute the above code, the output will be:
$ scalac Test.scala $ scala Test x is greater than 20
An else if...else statement can follow an if statement, which is very useful in the case of multiple conditional judgment statements.
The syntax format of if...else if...else statements is as follows:
if( boolean expression 1{ // If the boolean expression 1 Execute this statement block if true } else if( boolean expression 2{ // If the boolean expression 2 Execute this statement block if true } else if( boolean expression 3{ // If the boolean expression 3 Execute this statement block if true } else { // If all the above conditions are false, execute this statement block }
object Test { def main(args: Array[String]) { var x = 30; if( x == 10 { println("The value of X is 10"); } else if( x == 20 ){ println("The value of X is 20"); } else if( x == 30 ){ println("The value of X is 30"); } else { println("Unable to determine the value of X"); } } }
Execute the above code, the output will be:
$ scalac Test.scala $ scala Test The value of X is 30
Nested if...else statements can implement one or more if statements nested within an if statement.
The syntax format of nested if...else statements is as follows:
if( boolean expression 1{ // If the boolean expression 1 Execute this statement block if true if( boolean expression 2{ // If the boolean expression 2 Execute this statement block if true } }
Nested statements of else if...else are similar to nested statements of if...else.
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 will be:
$ scalac Test.scala $ scala Test X = 30 , Y = 10