Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions concepts/conditionals/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"authors": [
"colinleach"
],
"contributors": [],
"blurb": "The conditionals `if`, `else if` and `else` are used to control the flow of execution and make decisions in a program."
}
82 changes: 82 additions & 0 deletions concepts/conditionals/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# About conditionals

## Comparison operators

[Comparison operators][operators] are similar to many other languages, with a few extensions.

For equality, the operators are `==` (equal) and `!=` (not equal).

```Kotlin
val txt = "abc"
txt == "abc" // => true
txt != "abc" // => false
```

Additionally, the `===` and `!==` operators test for ["referential equality"][referential-equality]:
`a === b` if and only if `a` and `b` point to the same object.
This should make more sense later in the syllabus.

The greater/less than operators are also conventional.

```Kotlin
1 < 3 // => true
3 > 3 // => false
3 <= 3 // => true
4 >= 3 // => true
```

## Branching with `if`

This is the full form of an [`if` expression][if-else]:

```Kotlin
if (conditional1) {
// something...
} else if (conditional2) {
// something...
} else {
// something...
}
```

- Parentheses `()` around each conditional are required.
- A conditional must evaluate to a Boolean `true` or `false`.
Kotlin has no concept of "truthy" and "falsy" as found in some languages.
- Braces `{}` are optional, if there is only a single expression.
- Both `else if` and `else` are optional, and there can be multiple `else if` blocks.


## Alternatives?

By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java.
A concise form of `if ... else` is preferred:

```Kotlin
val result = if (isOk) goodValue else badValue

return if (isOK) goodValue else badValue
```

Unlike Ruby, the concise `if ... else` form *always* needs an `else` and thus cannot be used as a guard statement:

```kotlin
return 42 if (isOk)
// Syntax error: Unexpected tokens (use ';' to separate expressions on the same line).
// Syntax error: Expecting an expression.

return if (isOk) true
// 'if' must have both main and 'else' branches when used as an expression.
```

Note that in Kotlin, `if` is an [_expression_][expression] returning a value.
It is not a [_statement_][statement] as in Java.

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.


[operators]: https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols
[referential-equality]: https://kotlinlang.org/docs/equality.html#floating-point-numbers-equality
[if-else]: https://kotlinlang.org/docs/control-flow.html#if-expression
[when]: https://kotlinlang.org/docs/control-flow.html#when-expressions-and-statements
[expression]: https://en.wikipedia.org/wiki/Expression_(computer_science)
[statement]: https://en.wikipedia.org/wiki/Statement_(computer_science)
52 changes: 52 additions & 0 deletions concepts/conditionals/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Introduction

## Comparison operators

Comparison operators are similar to many other languages.

For equality, the operators are `==` (equal) and `!=` (not equal).

```Kotlin
val txt = "abc"
txt == "abc" // => true
txt != "abc" // => false
```

The greater/less than operators are also conventional.

```Kotlin
1 < 3 // => true
3 > 3 // => false
3 <= 3 // => true
4 >= 3 // => true
```

## Branching with `if`

This is the full form of an `if` expression:

```Kotlin
if (conditional1) {
// something...
} else if (conditional2) {
// something...
} else {
// something...
}
```

- Parentheses `()` around each conditional are required.
- A conditional must evaluate to a Boolean `true` or `false`.
Kotlin has no concept of "truthy" and "falsy" as found in some languages.
- Braces `{}` are optional, if there is only a single expression.
- Both `else if` and `else` are optional, and there can be multiple `else if` blocks.


## Alternatives?

By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java.
A concise form of `if ... else` is preferred:

```Kotlin
return if (isOK) goodValue else badValue
```
14 changes: 14 additions & 0 deletions concepts/conditionals/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"url": "https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols",
"description": "Operators reference."
},
{
"url": "https://kotlinlang.org/docs/control-flow.html#if-expression",
"description": "Control flow introduction."
},
{
"url": "https://kotlinlang.org/docs/coding-conventions.html#control-flow-statements",
"description": "Coding conventions."
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,11 @@
"uuid": "53e0b729-810e-4042-9477-8c8d05a7b302",
"slug": "booleans",
"name": "Booleans"
},
{
"uuid": "168827c0-4867-449a-ad22-611c87314c48",
"slug": "conditionals",
"name": "Conditionals"
}
],
"key_features": [
Expand Down