In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Parameterization 1: be careful: leave a blank line
© 2024 shulou.com SLNews company. All rights reserved.