- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 153字
- 2021-07-02 18:48:38
The while loop
The while loop repeats a block, while its conditional expression returns true:
while (condition) { //code }
There is also a do... while loop that repeats blocks as long as a conditional expression is returning true:
do { //code } while (condition)
Kotlin, as opposed to Java, can use variables declared inside the do... while loop as a condition:
do { var found = false //.. } while (found)
The main difference between the while and do... while loops is when a conditional expression is evaluated. A while loop checks the condition before code execution and if it is not true, the code won't be executed. On the other hand, a do... while loop first executes the body of the loop, and then evaluates the conditional expression, so the body will always execute at least once. If this expression is true, the loop will repeat. Otherwise, the loop terminates.
推薦閱讀
- 演進式架構(原書第2版)
- Elasticsearch for Hadoop
- H5頁面設計:Mugeda版(微課版)
- Python數據結構與算法(視頻教學版)
- BIM概論及Revit精講
- Angular開發入門與實戰
- C語言開發基礎教程(Dev-C++)(第2版)
- 小程序,巧應用:微信小程序開發實戰(第2版)
- INSTANT Adobe Edge Inspect Starter
- 從零開始:UI圖標設計與制作(第3版)
- 零基礎學Python編程(少兒趣味版)
- 零基礎學Scratch 3.0編程
- OpenCV 3.0 Computer Vision with Java
- Scrapy網絡爬蟲實戰
- Python深度學習入門:從零構建CNN和RNN