8.9. Scala IF…ELSE statement

发布时间 :2023-11-13 23:00:02 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.