Skip to content

Commit 345e67d

Browse files
colinleachColin Leach
andauthored
Add Conditionals concept (#710)
* Add Conditionals concept * Update about.md with Kotlin if...else details I like the suggestions, which are now added. --------- Co-authored-by: Colin Leach <[email protected]>
1 parent f7dba1a commit 345e67d

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [],
6+
"blurb": "The conditionals `if`, `else if` and `else` are used to control the flow of execution and make decisions in a program."
7+
}

concepts/conditionals/about.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Introduction
2+
3+
## Comparison operators
4+
5+
Comparison operators are similar to many other languages.
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+
The greater/less than operators are also conventional.
16+
17+
```Kotlin
18+
1 < 3 // => true
19+
3 > 3 // => false
20+
3 <= 3 // => true
21+
4 >= 3 // => true
22+
```
23+
24+
## Branching with `if`
25+
26+
This is the full form of an `if` expression:
27+
28+
```Kotlin
29+
if (conditional1) {
30+
// something...
31+
} else if (conditional2) {
32+
// something...
33+
} else {
34+
// something...
35+
}
36+
```
37+
38+
- Parentheses `()` around each conditional are required.
39+
- A conditional must evaluate to a Boolean `true` or `false`.
40+
Kotlin has no concept of "truthy" and "falsy" as found in some languages.
41+
- Braces `{}` are optional, if there is only a single expression.
42+
- Both `else if` and `else` are optional, and there can be multiple `else if` blocks.
43+
44+
45+
## Alternatives?
46+
47+
By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java.
48+
A concise form of `if ... else` is preferred:
49+
50+
```Kotlin
51+
return if (isOK) goodValue else badValue
52+
```

concepts/conditionals/links.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"url": "https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols",
4+
"description": "Operators reference."
5+
},
6+
{
7+
"url": "https://kotlinlang.org/docs/control-flow.html#if-expression",
8+
"description": "Control flow introduction."
9+
},
10+
{
11+
"url": "https://kotlinlang.org/docs/coding-conventions.html#control-flow-statements",
12+
"description": "Coding conventions."
13+
}
14+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,11 @@
13821382
"uuid": "53e0b729-810e-4042-9477-8c8d05a7b302",
13831383
"slug": "booleans",
13841384
"name": "Booleans"
1385+
},
1386+
{
1387+
"uuid": "168827c0-4867-449a-ad22-611c87314c48",
1388+
"slug": "conditionals",
1389+
"name": "Conditionals"
13851390
}
13861391
],
13871392
"key_features": [

0 commit comments

Comments
 (0)