Utilize Control Flow in Kotlin

May 22 2024 · Kotlin 1.9.23, Android 14, Kotlin Playground

Lesson 02: Leverage When Expressions

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 and get started.

    when {
      grade == 'A' -> println("Your grade is excellent")
      grade == 'B' -> println("You are very good")
      else -> {
        println("Your grade is lower than very good")
        println("Keep learning and improving!")
      }
    }
    when (grade) {
      'A' -> println("Your grade is excellent")
    }
    when (grade) {
      'A' -> println("Your grade is excellent")
      'B' -> println("You are very good")
      'C' -> println("Your grade is Good")
      'D' -> println("Your grade is Acceptable")
      'F' -> println("You failed")
    }
  else -> System.err.println("Unrecognized grade: $grade. Valid grades are A, B, C, D, and F.")
    when (val grade = args.firstOrNull()?.firstOrNull()) {
      'A' -> println("Your grade is excellent")
      'B' -> println("You are very good")
      'C' -> println("Your grade is Good")
      'D' -> println("Your grade is Acceptable")
      'F' -> println("You failed")
      else -> System.err.println("Unrecognized grade: $grade. Valid grades are A, B, C, D, and F.")
    }
    val result = when {
      grade == 'A' -> println("Your grade is excellent")
      grade == 'B' -> println("You are very good")
      else -> {
        println("Your grade is lower than very good")
        println("Keep learning and improving!")
      }
    }
    println(result)
    val message = when (grade) {
      'A' -> "Your grade is excellent"
      'B' -> "You are very good"
      'C' -> "Your grade is Good"
      'D' -> "Your grade is Acceptable"
      'F' -> "You failed"
      else -> {
        System.err.println("Unrecognized grade: $grade. Valid grades are A, B, C, D, and F.")
        "Your grade is unknown"
      }
    }
    println(message)
    val message = when (grade) {
      'A', 'B', 'C', 'D' -> "Your passed"
      'F' -> "You failed"
      else -> {
        System.err.println("Unrecognized grade: $grade. Valid grades are A, B, C, D, and F.")
        "Your grade is unknown"
      }
    }
    println(message)
    when {
      grade?.isLetter() == false -> System.err.println("Invalid grade: $grade")
      grade == 'A' -> println("Your grade is excellent")
    }
    val validGrades = setOf('A', 'B', 'C', 'D', 'F')
    when {
      grade in validGrades -> println("Your grade is valid")
    }
    val result = when {
      grade !in validGrades -> throw IllegalArgumentException("Invalid grade: $grade")
      grade == 'A' -> "Your grade is excellent"
      else -> "Your grade is valid"
    }
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion