当前位置:网站首页>Kotlin introductory notes (IV) circular statements (simple explanation of while, for)

Kotlin introductory notes (IV) circular statements (simple explanation of while, for)

2022-07-05 09:14:00 Meng Meng Mu Xi

Preface : This tutorial is best done with JAVA Study on the basis of

One 、while Use of statements

        while Use and java No different

Two 、for Use of statements

         Be careful :kotlin Give up for - i loop , take java Of for ( x : xx ) Turned into for - in loop ( It's written a bit like c#, But it's much more flexible )

So let's talk about that kotlin Characteristics of :

kotlin characteristic :

Section

  1. Closed interval at both ends    eg. val range = 0..10 ( Can take 0 To 10, Closed interval at both ends )
  2. Front open back close interval    eg. vla range = 0 until 10 ( You can get it. 0 To 9, Front opening and back closing )

step keyword

         By default ,for-in It will increase in the range 1. But if you want to jump some elements , have access to step keyword .

        Such as :

fun main() {
    for(i in 0 until 10 step 2) {
        println(i)
    }
}

 downTo keyword

        Create a descending space

fun main() {
    for(i in 10 downTo 1) {
        println(i)
    }
}

        for More about will be explained later ...

原网站

版权声明
本文为[Meng Meng Mu Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140538292560.html