Skip to content

Commit 4c12149

Browse files
committed
docs: revised 02_Loops.md
revised the text, split into sections
1 parent 3a9c8ee commit 4c12149

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

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. Looping 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. Executing the block while the condition is true.
58+
2. Executing the block first and then checking the condition.
5359

5460
### Iterators
5561

62+
You can define 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. Defining an iterator in a class. It must be named `iterator` and have the `operator` modifier.
90+
2. Returning the iterator that meets the following method requirements:
91+
* `next()`: `Animal`
92+
* `hasNext()`: `Boolean`
93+
3. Looping 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.

0 commit comments

Comments
 (0)