Learning Kotlin: Using For Loops

Another Learning Kotlin tutorial for beginners. Helping you learn Kotlin 1 small tutorial at a time.

In this video, you’ll learn the basics of Kotlin for loops, and see 6 different variations of using for loops in Kotlin.

📚 Reference Documentation


// basic for loop
fun printNames(names: Array<String>) {
    for(name in names) {
        println(name)
    }
}

// for loop with collection indicies
fun printNames(names: Array<String>) {
    for(index in names.indices) {
        println(names[index])
    }
}

// for loop with values & indicies
fun printNames(names: Array<String>) {
    for((index, name) in names.withIndex()) {
        println("Value at $index was $name")
    }
}

// loop using functional forEach
fun printNames(names: Array<String>) {
    names.forEach { name ->
        println(name)
    }
}

// loop using functional forEachIndexed
fun printNames(names: Array<String>) {
    names.forEachIndexed { index, name -> 
        println("Value at $index is $name")
    }
}

fun useIntRange() {
    for (i in 0..10) {
        println(i)
    }
}

I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Learning Kotlin: Using ‘if’ Expressions

Another Learning Kotlin tutorial for beginners. Helping you learn Kotlin 1 small tutorial at a time.

In this video, you’ll learn how to use if expressions and how to use if for logical control flow.

// basic control flow using if/else
fun printGreeting(name:String) {
    if(name.isNotBlank()) {
        println("hello $name")
    } else {
        println("hey there!")
    }
}

// Use 'if' as an expression in 
// a single expression function
//
fun getGreeting(age:Int) = if(age > 18) "hello" else "hey"

📚 Reference Documentation: https://kotlinlang.org/docs/reference/basic-syntax.html#using-conditional-expressions


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Learning Kotlin: How to Write a Single Expression Function in Kotlin

In this video, you’ll learn how to write a Single Expression Function in kotlin

📚 Reference Documentation: https://kotlinlang.org/docs/reference/functions.html#single-expression-functions

Thoughts or questions about single expression functions? Comment below or share on social media. I’d love to continue the conversation.


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Learning Kotlin: Learn to Write a Basic Kotlin Function

Another Learning Kotlin tutorial for beginners. Helping you learn Kotlin 1 small tutorial at a time.

In this video, you’ll learn Kotlin function basics. We’ll walk through how to write a basic Kotlin function and look at the ‘fun’ keyword, function names, and defining your function body.

 📚Reference: https://kotlinlang.org/docs/reference/functions.html


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Learning Kotlin: ‘lateinit’ Modifier

This Kotlin tutorial explores the lateinit modifier and gives several examples of how you can use lateinit in Android or other Kotlin projects.

lateinit allows you to defer initialization of non-null values so they can still be defined as non-null, but can be assigned later as in many dependency injection or test frameworks.

ex:

  • lateinit var userId:String

This is a non-null type, without a non-null initializer. Without lateinit, this would not be valid Kotlin code.

  • 0:17 What is lateinit used for?
  • 0:40 Why is lateinit useful?
  • 1:23 How to use lateinit?
  • 1:30 Android dependency injection example
  • 3:00 Testing example
  • 4:05 Where can lateinit be used?

Reference Documentation: https://kotlinlang.org/docs/reference…

Thoughts or questions about the lateinit modifier in Kotlin? Comment below or share on social media. I’d love to continue the conversation.


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel