- 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.
推薦閱讀
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- Drupal 8 Blueprints
- PHP 7底層設計與源碼實現
- GameMaker Programming By Example
- 重學Java設計模式
- Jenkins Continuous Integration Cookbook(Second Edition)
- Machine Learning in Java
- Building Microservices with .NET Core
- Test-Driven Development with Django
- 新一代SDN:VMware NSX 網絡原理與實踐
- 智能手機APP UI設計與應用任務教程
- HTML5移動Web開發
- JavaScript Unit Testing
- Python全棧開發:數據分析
- micro:bit軟件指南