Scala if...else Statement
The Scala if...else statement determines which block of code to execute based on the result (True or False) of one or more statements.
The following diagram gives a simple view of how conditional statements execute:

if statement
An if statement consists of a boolean expression followed by a block of statements.
Syntax
The syntax of an if statement is as follows:
if(布尔表达式)
{
// If the boolean expression is true, execute this code block
}
If the boolean expression is true, the block of statements inside the braces is executed; otherwise, that block is skipped and the statements after the braces are executed.
Example
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
x
=
10
;
if
(
x
<
20
)
{
println
("x < 20"
)
;
}
}
}
Run Example »
The output of the code above is:
$ scalac Test.scala
$ scala Test
x < 20
if...else statement
An if statement can be followed by an else statement, whose block of statements is executed when the boolean expression is false.
Syntax
The syntax of if...else is as follows:
if(布尔表达式){
// If the boolean expression is true, execute this code block
}else{
// If the boolean expression is false, execute this block
}
Example
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"
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
x 大于等于 20
if...else if...else statement
An if statement can be followed by an else if...else statement, which is useful when there are multiple conditional judgments.
Syntax
The syntax of if...else if...else is as follows:
if(布尔表达式 1){
// If the boolean expression 1 is true, execute this block
}else if(布尔表达式 2){
// If boolean expression 2 is true, execute this block
}else if(布尔表达式 3){
// If boolean expression 3 is true, execute this block
}else {
// If all the above conditions are false, execute this block
}
Example
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"
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
X 的值为 30
Nested if...else Statements
Nested if...else statements allow you to embed one or more if statements inside an if statement.
Syntax
The syntax of nested if...else statements is as follows:
if(布尔表达式 1){
// If the boolean expression 1 is true, execute this block
if(布尔表达式 2){
// If boolean expression 2 is true, execute this block
}
}
Nested else if...else statements are similar to nested if...else 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"
)
;
}
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
X = 30 , Y = 10