|
| 1 | +# About conditionals |
| 2 | + |
| 3 | +## Comparison operators |
| 4 | + |
| 5 | +[Comparison operators][operators] are similar to many other languages, with a few extensions. |
| 6 | + |
| 7 | +For equality, the operators are `==` (equal) and `!=` (not equal). |
| 8 | + |
| 9 | +```Kotlin |
| 10 | +val txt = "abc" |
| 11 | +txt == "abc" // => true |
| 12 | +txt != "abc" // => false |
| 13 | +``` |
| 14 | + |
| 15 | +Additionally, the `===` and `!==` operators test for ["referential equality"][referential-equality]: |
| 16 | +`a === b` if and only if `a` and `b` point to the same object. |
| 17 | +This should make more sense later in the syllabus. |
| 18 | + |
| 19 | +The greater/less than operators are also conventional. |
| 20 | + |
| 21 | +```Kotlin |
| 22 | +1 < 3 // => true |
| 23 | +3 > 3 // => false |
| 24 | +3 <= 3 // => true |
| 25 | +4 >= 3 // => true |
| 26 | +``` |
| 27 | + |
| 28 | +## Branching with `if` |
| 29 | + |
| 30 | +This is the full form of an [`if` expression][if-else]: |
| 31 | + |
| 32 | +```Kotlin |
| 33 | +if (conditional1) { |
| 34 | +// something... |
| 35 | +} else if (conditional2) { |
| 36 | +// something... |
| 37 | +} else { |
| 38 | +// something... |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +- Parentheses `()` around each conditional are required. |
| 43 | +- A conditional must evaluate to a Boolean `true` or `false`. |
| 44 | + Kotlin has no concept of "truthy" and "falsy" as found in some languages. |
| 45 | +- Braces `{}` are optional, if there is only a single expression. |
| 46 | +- Both `else if` and `else` are optional, and there can be multiple `else if` blocks. |
| 47 | + |
| 48 | + |
| 49 | +## Alternatives? |
| 50 | + |
| 51 | +By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java. |
| 52 | +A concise form of `if ... else` is preferred: |
| 53 | + |
| 54 | +```Kotlin |
| 55 | +val result = if (isOk) goodValue else badValue |
| 56 | + |
| 57 | +return if (isOK) goodValue else badValue |
| 58 | +``` |
| 59 | + |
| 60 | +Unlike Ruby, the concise `if ... else` form *always* needs an `else` and thus cannot be used as a guard statement: |
| 61 | + |
| 62 | +```kotlin |
| 63 | +return 42 if (isOk) |
| 64 | +// Syntax error: Unexpected tokens (use ';' to separate expressions on the same line). |
| 65 | +// Syntax error: Expecting an expression. |
| 66 | + |
| 67 | +return if (isOk) true |
| 68 | +// 'if' must have both main and 'else' branches when used as an expression. |
| 69 | +``` |
| 70 | + |
| 71 | +Note that in Kotlin, `if` is an [_expression_][expression] returning a value. |
| 72 | +It is not a [_statement_][statement] as in Java. |
| 73 | + |
| 74 | +We will see in a later Concept that Kotlin has a powerful [`when`][when] construct, intended to replace long chains of `else if` clauses with pattern matching. |
| 75 | + |
| 76 | + |
| 77 | +[operators]: https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols |
| 78 | +[referential-equality]: https://kotlinlang.org/docs/equality.html#floating-point-numbers-equality |
| 79 | +[if-else]: https://kotlinlang.org/docs/control-flow.html#if-expression |
| 80 | +[when]: https://kotlinlang.org/docs/control-flow.html#when-expressions-and-statements |
| 81 | +[expression]: https://en.wikipedia.org/wiki/Expression_(computer_science) |
| 82 | +[statement]: https://en.wikipedia.org/wiki/Statement_(computer_science) |
0 commit comments