avatarGabriel Shanahan

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

1304

Abstract

">Introduction</a> before moving on. Check out the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a> for all articles.</p></blockquote><p id="d2db">There is no ternary operator (i.e. <code><condition> ? <then> : <else></code>) in Kotlin. Instead, <code>if/else</code> may be used as an expression:</p> <figure id="aeb4"> <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%2FnTYgbwAmD&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FnTYgbwAmD&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><code>if</code> is an expression here — it returns a value. You can wrap each branch in <code>{</code> <code>}</code> if you need it to contain multiple statements.</li></ol><p id="cc86">This is actually much more powerful than the ternary operator, because you can also use <code>else if</code> branches:</p>

Options

   <figure id="e48e">
        <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%2FcAatB70DX&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FcAatB70DX&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="cdb1">In Java, you would need to nest the ternary operator, which quickly gets out of hand.</p><p id="2fd3">Go back to <a href="https://readmedium.com/loops-1d2b5ad83899">Loops</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/ranges-288a61ec080e">Ranges</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>

Conditional Expression

Using if/else as a replacement for the ternary operator in Kotlin

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

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 no ternary operator (i.e. <condition> ? <then> : <else>) in Kotlin. Instead, if/else may be used as an expression:

  1. if is an expression here — it returns a value. You can wrap each branch in { } if you need it to contain multiple statements.

This is actually much more powerful than the ternary operator, because you can also use else if branches:

In Java, you would need to nest the ternary operator, which quickly gets out of hand.

Go back to Loops, jump to the Table of Contents, or continue to Ranges.

Join me in Etnetera
Kotlin
Java
Programming
Learning
Ternary Operator
Recommended from ReadMedium