avatarGabriel Shanahan

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

1953

Abstract

p;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><ol><li>Iterates over a range starting from 0 up to 3 (inclusive). Like <code>for (i=0; i<=3; ++i)</code> in other C-based languages.</li><li>Iterates over a range starting from 0 up to 3 (exclusive). Like the <code>for</code> loop in Python or like <code>for(i=0; i< 3; ++i)</code> in other C-based languages.</li><li>Iterates over a range with a custom increment step for consecutive elements.</li><li>Iterates over a range in <i>reverse</i> order</li></ol><p id="b300">Now, this syntax might seem like it’s awfully ad-hoc, but in time, you’ll find out that everything here is actually something you can do yourself, using <a href="https://readmedium.com/operators-6625ca0b8a63">operators</a> and <a href="https://readmedium.com/infix-functions-f2c328941939">infix functions</a>. The <code>..</code> and <code>in</code> are simply syntactic sugar for calls to the <a href="https://kotlinlang.org/docs/operator-overloading.html#arithmetic-operators">rangeTo</a> and <a href="https://kotlinlang.org/docs/operator-overloading.html#in-operator">contains</a> operators, respectively. But all in due time — don’t worry about it for now.</p><p id="e634"><code>Char</code> ranges are also supported out of the box:</p> <figure id="9ee5"> <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%2Fr1i-nUnxF&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fr1i-nUnxF&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07

Options

&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><ol><li>Iterates over a char range in alphabetical order</li><li>Char ranges support <code>step</code> and <code>downTo</code> as well</li></ol><p id="a690">Ranges are also useful in <code>if</code> statements:</p> <figure id="9395"> <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%2FjSV6shBdK&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FjSV6shBdK&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>Checks if a value is in the range</li><li><code>!in</code> is the opposite of <code>in</code></li></ol><p id="5b53">More can be found in <a href="https://kotlinlang.org/docs/ranges.html">the docs</a>.</p><p id="5c38">Go back to the <a href="https://readmedium.com/conditional-expression-7fb52a02b767">Conditional Expression</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/triple-quoted-strings-adc99888ccb0">Triple-quoted Strings</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>

Ranges

Ranges in Kotlin, with a quick mention of the in operator, and associated infix functions — until, downTo and step

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

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.

There is a set of tools for working with ranges in Kotlin. Let’s take a quick look at them.

  1. Iterates over a range starting from 0 up to 3 (inclusive). Like for (i=0; i<=3; ++i) in other C-based languages.
  2. Iterates over a range starting from 0 up to 3 (exclusive). Like the for loop in Python or like for(i=0; i< 3; ++i) in other C-based languages.
  3. Iterates over a range with a custom increment step for consecutive elements.
  4. Iterates over a range in reverse order

Now, this syntax might seem like it’s awfully ad-hoc, but in time, you’ll find out that everything here is actually something you can do yourself, using operators and infix functions. The .. and in are simply syntactic sugar for calls to the rangeTo and contains operators, respectively. But all in due time — don’t worry about it for now.

Char ranges are also supported out of the box:

  1. Iterates over a char range in alphabetical order
  2. Char ranges support step and downTo as well

Ranges are also useful in if statements:

  1. Checks if a value is in the range
  2. !in is the opposite of in

More can be found in the docs.

Go back to the Conditional Expression, jump to the Table of Contents, or continue to Triple-quoted Strings.

Join me in Etnetera
Recommended from ReadMedium