Fragmented - Android Developer Podcast

150: Learning Kotlin - Returns, Jumps & Labels


Listen Later

Shownotes
  • Kotlin Returns and Jumps Documentation
  • Code
    data class Customer(val isPlatinum: Boolean)
    fun main() {
    val customer = Customer(false)
    println("Number of points customer has: ${calculatePoints(customer)}")
    // Break out of the loop once we're over 25
    for (i in 1..100) {
    if (i > 25) {
    break
    } else {
    println(i)
    }
    }
    // Skip all even numbers
    for (i in 1..100) {
    if (i % 2 == 0) {
    continue
    } else {
    println(i)
    }
    }
    // Break out of the outer loop (which breaks out of the inner too) using a label
    donn@ for (i in 1..100) {
    for (j in 100..200) {
    if (j > 150) break@donn // This will break out of the inner loop and outer loop
    else println("i: $i, j: $j")
    }
    }
    // Continue processing the next outer loop value when a condition is met.
    donn@ for (i in 1..100) {
    for (j in 100..200) {
    if (j > 150) continue@donn // This will break out of the inner loop and outer loop
    else println("i: $i, j: $j")
    }
    }
    // returns with label
    example1()
    example2()
    println("I'm done processing!")
    }
    fun calculatePoints(customer: Customer): Int {
    if (customer.isPlatinum) {
    return 100000
    } else {
    return 10
    }
    }
    fun example1() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach {
    if (it > 7) {
    return
    } else {
    println(it)
    }
    }
    println("This wont print :( because return exited the bar() function")
    }
    fun example2() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach bin@ {
    if (it > 7) {
    return@bin
    } else {
    println(it)
    }
    }
    println("This will print! :) return exited the forEach!")
    }
    Sponsors 🙏
    • Nevercode

      • Nevercode is taking Flutter revolution extremely serious and is prepared to offer kick-ass CI/CD for Flutter projects with codemagic.io. Check it out and get started at https://codemagic.io/
      • Sentry.io

        • Sentry tells you about errors in your code before your customers have a chance to encounter them.
        • Check them out at: https://sentry.io/for/android/
          Contact
          • Discord chat or @fragmentedcast or our Youtube channel
          • @donnfelker and donnfelker (on Instagram)
          • @kaushikgopal and kaushikgopal (on Instagram)
          • ...more
            View all episodesView all episodes
            Download on the App Store

            Fragmented - Android Developer PodcastBy Donn Felker, Kaushik Gopal

            • 5
            • 5
            • 5
            • 5
            • 5

            5

            68 ratings


            More shows like Fragmented - Android Developer Podcast

            View all
            Design Details by Brian Lovin, Marshall Bock

            Design Details

            361 Listeners

            Developer Tea by Jonathan Cutrell

            Developer Tea

            402 Listeners

            Does Not Compute by Sean Washington, Rockwell Schrock

            Does Not Compute

            53 Listeners

            Toolsday by Una Kravets, Chris Dhanaraj

            Toolsday

            48 Listeners

            Swift Unwrapped by JP Simard, Jesse Squires, Spec Network, Inc.

            Swift Unwrapped

            90 Listeners