3.9. Kotlin cycle control

发布时间 :2023-10-12 23:00:04 UTC      

3.9.1. For cycle #

for loop can provide an iterator for any iterator syntax isas follows:

for (item in collection) print(item)

The body of the loop can be a code block:

for (item: Int in ints) {
    // ……
}

As mentioned above for you can loop through any object that provides aniterator.

If you want to traverse an array or an list you can do this:

for (i in array.indices) {
    print(array[i])
}

Note that this “traversal on the interval” is compiled into an optimized implementation without creating additional objects.

Or you can use library functions withIndex :

for ((index, value) in array.withIndex()) {
    println("the element at $index is $value")
}

Example #

Iterate over the collection:

fun main(args: Array<String>) {
    val items = listOf("apple", "banana", "kiwi")
    for (item in items) {
        println(item)
    }

    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }
}

Output result:

apple
banana
kiwi
item at 0 is apple
item at 1 is banana
item at 2 is kiwi

3.9.2. While and do…while cycle #

while is the most basic loop, and its structure is:

While (Boolean expression) {
  //Circular Content
}

do…while loop for while statement, you cannot enter a loop if the condition is not met. But sometimes we need to execute at least once, even if the conditions are not met.

do…while cyclic and while cycle is similar, except that do…while loop is executed at least once.

do {
       //code statement
}While (Boolean expression);

Example #

fun main(args: Array<String>) {
    println("----while use-----")
    var x = 5
    while (x > 0) {
        println( x--)
    }
    println("----do...while use-----")
    var y = 5
    do {
        println(y--)
    } while(y>0)
}

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.