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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FnTYgbwAmD&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><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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FcAatB70DX&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&utm_medium=GabrielShanahan&utm_campaign=KotlinPrimer&utm_content=join-our-team&utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>