avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1512

Abstract

oops: <code>for</code>, <code>while</code>, <code>do-while</code></p><h1 id="e9b5">For</h1><p id="084c"><code>for</code> in Kotlin works the same way as the <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">enhanced <code></code>for statement</a> in Java.</p> <figure id="4809"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FsJChKhWS0&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FsJChKhWS0&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><ol><li>Loops through each cake in the list.</li><li>We’ll get to <a href="https://readmedium.com/string-templates-92eb82841cbb">string templates</a> (i.e. the <code>$cake</code> part) in a bit</li></ol><p id="a6b4">You might be wondering how to emulate the classic <code>for</code> loop, e.g. <code>for(int i = 0; i < 10; i++)</code>. This can be achieved using the above form in combination with <a href="https://readmedium.com/ranges-288a61ec080e">ranges</a>, which we’ll talk about soon.</p><h1 id="bb25">while and do-while</h1><p id="2a7f"><code>while</code> and <code>do-while</code> con

Options

structs work similarly to most languages as well.</p> <figure id="a061"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FN8rQ3-1cX&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FN8rQ3-1cX&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><ol><li>Executes the block while the condition is true.</li><li>Executes the block first and then checks the condition.</li></ol><p id="b7a1">Go back to the <a href="https://readmedium.com/when-construct-c90606274897">When Construct</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to the <a href="https://readmedium.com/conditional-expression-7fb52a02b767">Conditional Expression</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Loops

A quick look at looping in Kotlin, covering for, while and do-while

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Kotlin supports all the commonly used loops: for, while, do-while

For

for in Kotlin works the same way as the enhanced for statement in Java.

  1. Loops through each cake in the list.
  2. We’ll get to string templates (i.e. the $cake part) in a bit

You might be wondering how to emulate the classic for loop, e.g. for(int i = 0; i < 10; i++). This can be achieved using the above form in combination with ranges, which we’ll talk about soon.

while and do-while

while and do-while constructs work similarly to most languages as well.

  1. Executes the block while the condition is true.
  2. Executes the block first and then checks the condition.

Go back to the When Construct, jump to the Table of Contents, or continue to the Conditional Expression.

Join me in Etnetera
Kotlin
Java
Programming
Learning
Loops
Recommended from ReadMedium