mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
[kotlin/en] Add "if" usage as an expression (#2367)
* [kotlin/en] Add "if" usage as a function * [kotlin/en] Change comment on "if" expression Rephrase the comment to use the term "expression" in place of "function"
This commit is contained in:
parent
71b3342f04
commit
161edb1f6e
@ -261,6 +261,14 @@ fun helloWorld(val name : String) {
|
|||||||
ctr++
|
ctr++
|
||||||
} while (ctr < 10)
|
} while (ctr < 10)
|
||||||
|
|
||||||
|
/*
|
||||||
|
"if" can be used as an expression that returns a value.
|
||||||
|
For this reason the ternary ?: operator is not needed in Kotlin.
|
||||||
|
*/
|
||||||
|
val num = 5
|
||||||
|
val message = if (num % 2 == 0) "even" else "odd"
|
||||||
|
println("$num is $message") // => 5 is odd
|
||||||
|
|
||||||
// "when" can be used as an alternative to "if-else if" chains.
|
// "when" can be used as an alternative to "if-else if" chains.
|
||||||
val i = 10
|
val i = 10
|
||||||
when {
|
when {
|
||||||
|
Loading…
Reference in New Issue
Block a user