Skip to content

Commit f612624

Browse files
Merge pull request Kotlin#45 from p7nov/controlflow-revised
docs: revised Control Flow
2 parents 355ef52 + e964ee3 commit f612624

File tree

4 files changed

+114
-44
lines changed

4 files changed

+114
-44
lines changed
Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# When
22

3-
Kotlin does not have `switch` statement, instead of it it has more smart and clear `when` construction. Let's take a look
3+
Instead of the widely used `switch` statement, Kotlin provides the more flexible and clear `when` construction. It can be used either as a statement or as an expression.
4+
5+
## When Statement
46

57
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
68

@@ -13,24 +15,57 @@ fun main() {
1315
cases("hello")
1416
}
1517

16-
fun cases(obj: Any) {
18+
fun cases(obj: Any) { // 1
1719
when (obj) {
18-
1 -> println("One") // 1
19-
"Hello" -> println("Greeting") // 2
20-
is Long -> println("Long") // 3
21-
!is String -> println("Not a string") // 4
22-
else -> println("Unknown") // 5
23-
}
20+
1 -> println("One") // 2
21+
"Hello" -> println("Greeting") // 3
22+
is Long -> println("Long") // 4
23+
!is String -> println("Not a string") // 5
24+
else -> println("Unknown") // 6
25+
}
2426
}
2527

2628
class MyClass
2729
```
2830

2931
</div>
3032

33+
1. This is a `when` statement.
34+
2. Checks whether `obj` equals to one.
35+
3. Checks whether `obj` equals to `Hello`.
36+
4. Performs type checking.
37+
5. Performs inverse type checking.
38+
6. Default statement (might be omitted).
39+
40+
Note that all branch conditions are checked sequentially until one of them is satisfied. So, only the first suitable branch will be executed.
41+
42+
## When Expresion
43+
44+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
45+
46+
```kotlin
47+
fun main() {
48+
println(whenAssign("Hello"))
49+
println(whenAssign(3.4))
50+
println(whenAssign(1))
51+
println(whenAssign(MyClass()))
52+
}
53+
54+
fun whenAssign(obj: Any): Any {
55+
val result = when (obj) { // 1
56+
1 -> "one" // 2
57+
"Hello" -> 1 // 3
58+
is Long -> false // 4
59+
else -> 42 // 5
60+
}
61+
return result
62+
}
63+
```
64+
65+
</div>
3166

32-
1. Check whether `obj` equals to `1`
33-
2. Check for string
34-
3. Or even instance check
35-
4. Negative instance check
36-
5. Default statement (might be omitted)
67+
1. This is a `when` expression.
68+
2. Sets the value to `"one"` if `obj` equals to one.
69+
3. Sets the value to one if `obj` equals to `Hello`.
70+
4. Sets the value to `false` if `obj` is an instance of `Long`.
71+
5. Sets the value "42" if none of the previous conditions are satisfied. Unlike in `when` _statement_, the default branch is usually required in `when` _expression_, except the case when the compiler can check that other branches cover all possible cases.

examples/02_control_flow/02_Loops.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#Loops
22

3-
### for, while, do-while
3+
Kotlin supports all the commonly used loops: `for`, `while`, `do-while`
4+
5+
### `for`
6+
7+
`for` in Kotlin works the same way as in most languages.
48

59
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
610

@@ -19,7 +23,11 @@ fun main(args: Array<String>) {
1923

2024
</div>
2125

22-
1. Loops through each cake in the list
26+
1. Loops through each cake in the list.
27+
28+
### `while` and `do-while`
29+
30+
`while` and `do-while` constructs work similarly to most languages as well.
2331

2432
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
2533

@@ -46,13 +54,13 @@ fun main(args: Array<String>) {
4654

4755
</div>
4856

49-
While and do-while constructs work similarly to most languages.
50-
51-
1. Performs the block while the condition is true.
52-
2. Performs the block first, and then loops while evaluating the while condition.
57+
1. Executes the block while the condition is true.
58+
2. Executes the block first and then checking the condition.
5359

5460
### Iterators
5561

62+
You can define your own iterators in your classes by implementing the `iterator` operator in them.
63+
5664
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
5765

5866
```kotlin
@@ -78,10 +86,10 @@ fun main() {
7886

7987
</div>
8088

81-
1. Providing an iterator implementation (marked with operator modifier).
82-
2. Returns the iterator for the list, which meets the method requirements
83-
1. next(): Animal
84-
2. hasNext(): Boolean
85-
3. Loops through each animal in the zoo
89+
1. Defines an iterator in a class. It must be named `iterator` and have the `operator` modifier.
90+
2. Returns the iterator that meets the following method requirements:
91+
* `next()`: `Animal`
92+
* `hasNext()`: `Boolean`
93+
3. Loops through animals in the zoo with the user-defined iterator.
8694

87-
The iterator can be declared on the type or as an extension function.
95+
The iterator can be declared in the type or as an extension function.
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Ranges
22

3+
There is a set of tools for defining ranges in Kotlin. Let's have a brief look at them.
4+
35
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
46

57
```kotlin
@@ -15,29 +17,61 @@ fun main() {
1517
}
1618
print(" ")
1719

18-
for (c in 'a'..'d') { // 3
19-
print(c)
20+
for (i in 3 downTo 0) { // 3
21+
print(i)
2022
}
2123
print(" ")
2224

23-
for (c in 'a'..'g' step 2) { // 4
25+
//sampleEnd
26+
}
27+
```
28+
29+
</div>
30+
31+
1. Iterates over a range starting from 0 up to 3 (inclusive).
32+
2. Iterates over a range with a custom increment step for consecutive elements.
33+
5. Iterates over a range in _reverse_ order.
34+
35+
Char ranges are also supported:
36+
37+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
38+
39+
```kotlin
40+
fun main() {
41+
//sampleStart
42+
for (c in 'a'..'d') { // 1
2443
print(c)
2544
}
2645
print(" ")
2746

28-
for (i in 3 downTo 0) { // 5
29-
print(i)
47+
for (c in 'z' downTo 's' step 2) { // 2
48+
print(c)
3049
}
3150
print(" ")
3251

52+
//sampleEnd
53+
}
54+
```
55+
56+
</div>
57+
58+
1. Iterates over a char range in alphabetical order.
59+
2. Char ranges support `step` and `downTo` as well.
60+
61+
Ranges are also useful in `if` statements:
62+
63+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
64+
65+
```kotlin
66+
fun main() {
67+
//sampleStart
3368
val x = 2
34-
if (x in 1..10) { // 6
69+
if (x in 1..10) { // 1
3570
print(x)
3671
}
3772
print(" ")
3873

39-
val y = 3
40-
if (y !in 1..4) { // 7
74+
if (x !in 1..4) { // 2
4175
print(y)
4276
}
4377
//sampleEnd
@@ -46,11 +80,5 @@ fun main() {
4680

4781
</div>
4882

49-
1. Create range starting from 0 up to 3 (inclusive) and iterate over it
50-
2. You can define increment step for consecutive elements
51-
3. Char ranges can be created as well
52-
4. Increment step for char ranges are also valid
53-
5. To create a range in _reverse_ order use `downTo()` function
54-
6. Ranges are also useful in `if` statements
55-
7. `!in` is opposite of `in`. Statement is equal to `_false_`
56-
83+
1. Checks if a value is in the range.
84+
2. `!in` is the opposite of `in`.

examples/02_control_flow/05_Conditional expression.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Conditional Expression
22

3-
There is no ternary operator (condition ? then : else) in Kotlin instead `if` expression could be used in the same way.
4-
Let's take a look
3+
There is no ternary operator `condition ? then : else` in Kotlin. Instead, `if` may be used as an expression:
54

65
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
76

@@ -17,4 +16,4 @@ fun main() {
1716

1817
</div>
1918

20-
1. `if` is an expression, i.e. it returns a value.
19+
1. `if` is an expression here: it returns a value.

0 commit comments

Comments
 (0)