Scala IF...ELSE Statements
The Scala IF...ELSE statement determines which block of code to execute based on the result (True or False) of one or more statements.
You can get a simple understanding of the execution process of conditional statements through the following diagram:
if Statement
The if statement consists of a boolean expression and the subsequent block of statements.
Syntax
The syntax of the if statement is as follows:
if(boolean expression)
{
// Execute this block of statements if the boolean expression is true
}
If the boolean expression is true, the block of statements within the braces is executed; otherwise, the block within the braces is skipped, and the block after the braces is executed.
Example
object Test {
def main(args: Array[String]) {
var x = 10;
if( x < 20 ){
println("x < 20");
}
}
}
When the above code is executed, the output is:
$ scalac Test.scala
$ scala Test
x < 20
if...else Statement
An else statement can immediately follow an if statement, and the block of statements within the else can be executed when the boolean expression is false.
Syntax
The syntax of the if...else is as follows:
if(boolean expression){
// Execute this block of statements if the boolean expression is true
}else{
// Execute this block of statements if the boolean expression is false
}
Example
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 or equal to 20");
}
}
}
When the above code is executed, the output is:
$ scalac Test.scala
$ scala Test
x is greater than or equal to 20
if...else if...else Statement
An else if...else statement can immediately follow an if statement, which is very useful in situations with multiple conditional statements.
Syntax
The syntax of the if...else if...else is as follows:
if(boolean expression 1){
// Execute this block of statements if boolean expression 1 is true
}else if(boolean expression 2){
// Execute this block of statements if boolean expression 2 is true
}else if(boolean expression 3){
// Execute this block of statements if boolean expression 3 is true
}else {
// Execute this block of statements if all the above conditions are false
}
Example
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("Cannot determine the value of X");
}
}
}
When the above code is executed, the output is:
$ scalac Test.scala
$ scala Test
The value of X is 30
if...else Nested Statements
if...else nested statements can achieve the embedding of one or more if statements within an if statement.
Syntax
The syntax of if...else nested statements is as follows:
if(boolean expression 1){
// Execute this block of statements if boolean expression 1 is true
if(boolean expression 2){
// Execute this block of statements if boolean expression 2 is true
}
}
The nesting of else if...else statements is 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");
}
}
}
}
When the above code is executed, the output is:
$ scalac Test.scala
$ scala Test
X = 30 , Y = 10