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:
The If the Boolean expression is Execute the above code, and the output is as follows: The Execute the above code, and the output is as follows: The Execute the above code, and the output is as follows: The Execute the above code, and the output is as follows:
8.9.1. If statement #
if
statements consist of Boolean expressions and subsequent statement blocks. 8.9.2. Grammar #
if
syntax format of the statement is as follows:If (Boolean expression)
{
//If the Boolean expression is true, execute the statement block
}
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. 8.9.3. Example #
Example #
object Test {
def main(args: Array[String]) {
var x = 10;
if( x < 20 ){
println("x < 20");
}
}
}
$ scalac Test.scala
$ scala Test
x < 20
8.9.4. 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. 8.9.5. Grammar #
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
}
8.9.6. Example #
Example #
object Test {
def main(args: Array[String]) {
var x = 30;
if( x < 20 ){
println("x < 20");
}else{
println("x > 20");
}
}
}
$ scalac Test.scala
$ scala Test
x > 20
8.9.7. If…else if…else statement #
if
statement can be followed by
else
statement, which is useful in the case of multiple conditional judgment statements. 8.9.8. Grammar #
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
}
8.9.9. 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");
}
}
}
$ scalac Test.scala
$ scala Test
The value of X is 30
8.9.10. If…else nested statement #
if...else
nested statements can be implemented in the
if
statement to embed one or more
if
statement. 8.9.11. Grammar #
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. 8.9.12. 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");
}
}
}
}
$ scalac Test.scala
$ scala Test
X = 30 , Y = 10