- 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.
推薦閱讀
- 自然語言處理實戰:預訓練模型應用及其產品化
- JMeter 性能測試實戰(第2版)
- INSTANT MinGW Starter
- 深入理解Java7:核心技術與最佳實踐
- Hands-On Swift 5 Microservices Development
- SQL Server從入門到精通(第3版)
- 青少年信息學競賽
- Cocos2d-x Game Development Blueprints
- Kubernetes進階實戰
- 視窗軟件設計和開發自動化:可視化D++語言
- C語言程序設計與應用實驗指導書(第2版)
- Ubuntu Server Cookbook
- PHP程序設計高級教程
- 開發者測試
- BackTrack 5 Cookbook