8.9. Scala IF…ELSE statement

发布时间 : 2025-10-25 13:35:12 UTC      

Page Views: 10 views

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:

Image0

8.9.1. If statement #

if statements consist of Boolean expressions and subsequent statement blocks.

8.9.2. Grammar #

The if syntax format of the statement is as follows:

If (Boolean expression) { //If the Boolean expression is true, execute the statement block } 

If the Boolean expression is 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"); } } } 

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

$ 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 #

The 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"); } } } 

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

$ 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 #

The 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"); } } } 

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

$ 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 #

The 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"); } } } } 

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

$ scalac Test.scala $ scala Test X = 30 , Y = 10 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。