Skip to content

Commit 55537f9

Browse files
chore(examples): add Control structures examples
1 parent 72b9e08 commit 55537f9

File tree

10 files changed

+201
-203
lines changed

10 files changed

+201
-203
lines changed

examples/02_advanced/00_description.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/02_advanced/01_namedArguments.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/02_advanced/02_extensionFunctions.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/02_advanced/03_Data classes.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/02_advanced/04_Destructuring Declarations.md

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Control structures
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# When
2+
3+
Kotlin do not have `switch` statement, instead of it it has more smart and clear `when` contrusction. Let's take a look
4+
5+
<div class="language-kotlin" theme="idea">
6+
```kotlin
7+
fun main() {
8+
cases("Hello")
9+
cases(1)
10+
cases(0L)
11+
cases(MyClass())
12+
cases("hello")
13+
}
14+
15+
fun cases(obj: Any) {
16+
when (obj) {
17+
1 -> println("One") // 1
18+
"Hello" -> println("Greeting") // 2
19+
is Long -> println("Long") // 3
20+
!is String -> println("Not a string") // 4
21+
else -> println("Unknown") // 5
22+
}
23+
}
24+
25+
class MyClass() {
26+
}
27+
```
28+
</div>
29+
30+
31+
1. Check whether `obj` equals to `1`
32+
2. Check for string
33+
3. Or even instance check
34+
4. Negative instance check
35+
5. Default statement (might be omitted)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#Loops
2+
3+
### for, while, do-while
4+
5+
<div class="language-kotlin" theme="idea">
6+
7+
```kotlin
8+
fun main() {
9+
//sampleStart
10+
val cakes = listOf("carrot", "cheese", "chocolate")
11+
12+
for (cake in cakes) { // 1
13+
println("Yummy, it's a $cake cake!")
14+
}
15+
16+
//sampleEnd
17+
}
18+
```
19+
20+
</div>
21+
22+
1. Loops through each cake in the list
23+
24+
<div class="language-kotlin" theme="idea">
25+
26+
```kotlin
27+
//sampleStart
28+
fun eatACake() = println("Eat a Cake")
29+
fun bakeACake() = println("Bake a Cake")
30+
31+
fun main() {
32+
var cakesEaten = 0
33+
var cakesBaked = 0
34+
35+
while (cakesEaten < 5) { // 1
36+
eatACake()
37+
cakesEaten ++
38+
}
39+
40+
do { // 2
41+
bakeACake()
42+
cakesBaked++
43+
} while (cakesBaked < cakesEaten)
44+
45+
}
46+
//sampleEnd
47+
```
48+
49+
</div>
50+
51+
While and do-while constructs work similarly to most languages.
52+
53+
1. Performs the block while the condition is false
54+
2. Performs the block first, and then loops while evaluating the while condition.
55+
56+
### Iterators
57+
58+
<div class="language-kotlin" theme="idea">
59+
60+
```kotlin
61+
class Animal(val name: String)
62+
63+
class Zoo(val animals: List<Animal>) {
64+
65+
operator fun iterator(): Iterator<Animal> { // 1
66+
return animals.iterator() // 2
67+
}
68+
}
69+
70+
fun main() {
71+
72+
val zoo = Zoo(listOf(Animal("zebra"), Animal("lion")))
73+
74+
for (animal in zoo) { // 3
75+
println("Watch out, it's a ${animal.name}")
76+
}
77+
78+
}
79+
```
80+
81+
</div>
82+
83+
1. Providing an iterator implementation (marked with operator modifier).
84+
2. Returns the iterator for the list, which meets the method requirements
85+
1. next(): Animal
86+
2. hasNext(): Boolean
87+
3. Loops through each animal in the zoo
88+
89+
The iterator can be declared on the type or as an extension function.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Ranges
2+
3+
<div class="language-kotlin" theme="idea">
4+
5+
```kotlin
6+
fun main() {
7+
//sampleStart
8+
for(i in 0..3) { // 1
9+
print(i)
10+
}
11+
print(" ")
12+
13+
for(i in 2..8 step 2) { // 2
14+
print(i)
15+
}
16+
print(" ")
17+
18+
for (c in 'a'..'d') { // 3
19+
print(c)
20+
}
21+
print(" ")
22+
23+
for (c in 'a'..'g' step 2) { // 4
24+
print(c)
25+
}
26+
print(" ")
27+
28+
for (i in 3 downTo 0) { // 5
29+
print(i)
30+
}
31+
print(" ")
32+
33+
val x = 2
34+
if (x in 1..10) { // 6
35+
print(x)
36+
}
37+
print(" ")
38+
39+
val y = 3
40+
if (y !in 1..4) { // 7
41+
print(y)
42+
}
43+
//sampleEnd
44+
}
45+
```
46+
47+
</div>
48+
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 oposite of `in`. Statement is equal to `_false_`
56+

0 commit comments

Comments
 (0)