Meet the Kotlin Programming Language

Apr 10 2024 · Kotlin 1.9, Android 14, Android Studio Hedgehog

Lesson 04: Leverage Control Flows

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Open the Starter project in the 04-leverage-control-flows directory of the m3-kpl-materials repo in Android Studio Hedgehog or later.

// 1
val age = 1

// 2
if (age < 12 ) {
 // 3
 println("You can't vote")
} else if (age < 18) {
 println("You can vote soon")
} else {
 print("You can vote")
}
// 1
println("Welcome to Kotlin Snack Corner!")
println("Please select an item by entering the number:")
println("1. Burger\n2. Pizza\n3. Sandwich\n4. Fries")

// 2
val choice = 4

// 3 & 4
val preparationTime = when (choice) {
    1 -> "Burger will take 10 minutes to prepare."
    2 -> "Pizza will take 15 minutes to prepare."
    3 -> "Sandwich will take 5 minutes to prepare."
    4 -> "Fries will take 7 minutes to prepare."
    else -> "Sorry, we don't have that item on our menu."
}

// 5
println(preparationTime)
var lyric = "Haters gonna"

for (i in 1..5) {
    lyric += " hate"
}

println(lyric)
var counter = 10
while (counter >= 0) {
    println("T-$counter")
    counter--
}

println("We have lift off \uD83D\uDE80")

var counter = 10
do {
    println("T-$counter")
    counter--
} while (counter >= 0)
println("We have lift off \uD83D\uDE80")
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion