- 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.
推薦閱讀
- HornetQ Messaging Developer’s Guide
- Java多線程編程實戰(zhàn)指南:設(shè)計模式篇(第2版)
- Java系統(tǒng)分析與架構(gòu)設(shè)計
- Oracle Exadata性能優(yōu)化
- Practical Data Science Cookbook(Second Edition)
- Elastic Stack應(yīng)用寶典
- Java Web開發(fā)技術(shù)教程
- Building a Quadcopter with Arduino
- SQL Server與JSP動態(tài)網(wǎng)站開發(fā)
- Learning R for Geospatial Analysis
- JavaScript動態(tài)網(wǎng)頁編程
- SignalR:Real-time Application Development(Second Edition)
- Mobile Forensics:Advanced Investigative Strategies
- R語言實戰(zhàn)(第2版)
- Building a Media Center with Raspberry Pi