Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the IF ELSE statement of Scala

Shulou Source: shulou.com Published: 2022-06-01 16:23:48 10月05日 Update

This article mainly explains "how to use the IF ELSE sentence of Scala". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the IF ELSE sentence of Scala.

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:

If statement

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

Grammar

The syntax format of the if statement is as follows:

If (Boolean expression)

{

/ / execute the statement block if the Boolean expression is true

}

Execute the statement block within the curly braces if the Boolean expression is true, otherwise skip the statement block within the braces and execute the statement block after the curly braces.

Instance object Test {

Def main (args: Array [String]) {

Var x = 10

If (x < 20) {

Println ("x < 20")

}

}

}

Execute the above code, and the output is as follows:

$scalac Test.scala

$scala Test

X < 20

If...else statement

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

Grammar

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

If (Boolean expression) {

/ / execute the statement block if the Boolean expression is true

} else {

/ / execute the statement block if the Boolean expression is false

}

Instance object Test {

Def main (args: Array [String]) {

Var x = 30

If (x < 20) {

Println ("x less than 20")

} else {

Println ("x greater than 20")

}

}

}

Execute the above code, and the output is as follows:

$scalac Test.scala

$scala Test

X greater than 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.

Grammar

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

If (Boolean expression 1) {

/ / execute the statement block if Boolean expression 1 is true

} else if (Boolean expression 2) {

/ / execute the statement block if Boolean expression 2 is true

} else if (Boolean expression 3) {

/ / execute the statement block if Boolean expression 3 is true

} else {

/ / if all the above conditions are for false to execute the statement block

}

Instance object Test {

Def main (args: Array [String]) {

Var x = 30

If (x = = 10) {

Println ("X value is 10")

} else if (x = = 20) {

Println ("X value is 20")

} else if (x = = 30) {

Println ("value of X is 30")

} else {

Println ("unable to determine the value of X")

}

}

}

Execute the above code, and the output is as follows:

$scalac Test.scala

$scala Test

The value of X is 30

If...else nested statement

If...else nested statements can embed one or more if statements within an if statement.

Grammar

The syntax format of if...else nested statements is as follows:

If (Boolean expression 1) {

/ / execute the statement block if Boolean expression 1 is true

If (Boolean expression 2) {

/ / execute the statement block if Boolean expression 2 is true

}

}

Else if...else 's nested statements are similar to if...else nested statements.

Instance 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, and the output is as follows:

$scalac Test.scala

$scala Test

X = 30, Y = 10 Thank you for your reading, the above is the content of "how to use the IF ELSE sentence of Scala". After the study of this article, I believe you have a deeper understanding of how to use the IF ELSE sentence of Scala. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Statements Boolean expressions syntax code results examples formats output parentheses conditions learning content multiple case the following picture can be passed multiple that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Shulou Technology Redmi Apple