Lesson 20 of 100
Control Flow Practice
Combining control flow concepts
📚
Learn💻
Practice❓
Quiz🏆
Challenge⭐
Complete📚 Lý thuyết
Bài này tổng hợp tất cả kiến thức về **Control Flow**: **Đã học:** - if/else expressions - when expressions - for loops (ranges, collections) - while và do-while loops - break và continue - Ranges **Best Practices:** - Dùng when thay vì if-else chain dài - Dùng for với ranges thay vì while khi biết số lần lặp - Tránh nested loops sâu - Dùng early return thay vì nested if
💡 Ví dụ 1/3
Game Loop Pattern
🟣 kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fun main() { var health = 100 var round = 1 while (health > 0) { println("\n=== Round $round ===") println("Health: $health") val damage = (10..30).random() health -= damage println("Took $damage damage!") if (health <= 0) { println("Game Over!") break } val heal = if ((1..4).random() == 1) { val amount = (5..15).random() println("Healed $amount!") amount } else 0 health += heal round++ } println("Survived $round rounds!")}💬 Kết hợp while, if, when, random trong game loop.
🎯 Fun Fact
Kotlin được thiết kế để viết code ngắn gọn - cùng một logic trong Kotlin thường ngắn hơn Java 40%!
💡 Pro Tip
Khi code phức tạp, chia nhỏ thành functions riêng để dễ đọc và test!