Language Elements

Data Types

Kotlin Nullable Types

Exctension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Coroutines

Collections

Kotlin Example Codes

Kotlin Interview Questions

Kotlin Codinf Samples

Kotlin Conditional Loops

For Loops

The for loop is used to iterate over ranges, arrays, or collections.

    
fun main() {
    for (i in 1..5) {
        println("Count: $i")
    }
}
    

This code iterates from 1 to 5 and prints the numbers. The 1..5 represents a range from 1 to 5.

Using For Loop to Iterate Over an Array

    
fun main() {
    val fruits = arrayOf("Apple", "Banana", "Cherry")
    for (fruit in fruits) {
        println(fruit)
    }
}
    

This code iterates through all elements in the array and prints each fruit.

Using For Loop to Iterate Over Indices

    
fun main() {
    val numbers = arrayOf(10, 20, 30, 40)
    for (index in numbers.indices) {
        println("Index $index: ${numbers[index]}")
    }
}
    

This example uses the indices property to iterate over the indices of an array and access elements using their index.

While Loops

The while loop executes as long as the condition is true.

    
fun main() {
    var counter = 1
    while (counter <= 5) {
        println("Counter: $counter")
        counter++
    }
}
    

Here, the loop increments the counter from 1 to 5 and prints the values.

Do-While Loops

The do-while loop guarantees that the code executes at least once.

    
fun main() {
    var counter = 1
    do {
        println("Counter: $counter")
        counter++
    } while (counter <= 5)
}
    

This code prints the counter values from 1 to 5, even if the condition fails after the first iteration.

When Expression

The when expression in Kotlin is similar to switch in Java. It evaluates a value and executes the corresponding branch.

    
fun main() {
    val number = 2
    when (number) {
        1 -> println("One")
        2 -> println("Two")
        3 -> println("Three")
        else -> println("Unknown number")
    }
}
    

This code checks the value of number and prints the appropriate message. The else branch is equivalent to the default case in Java.

Using when with Type Checking

    
fun main() {
    val input: Any = "Hello"
    when (input) {
        is String -> println("It's a String: $input")
        is Int -> println("It's an Integer: $input")
        else -> println("Unknown type")
    }
}
    

Here, is is used for type checking within the when expression.

Kotlin Basic Data Structures

Arrays

In Kotlin, arrays can store data of the same type (most common) or mixed types (less common), but the number of elements is fixed once the array is created.

    
fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    for (num in numbers) {
        println("Number: $num")
    }
}
    

This code defines an array of integers and iterates through its elements.

Lists

Lists are used to store ordered collections of elements.

    
fun main() {
    val fruits = listOf("Apple", "Banana", "Cherry")
    for (fruit in fruits) {
        println("Fruit: $fruit")
    }
}
    

The listOf function creates an immutable list of fruits.

Sets

Sets are collections that contain unique elements.

    
fun main() {
    val numbers = setOf(1, 2, 2, 3)
    for (num in numbers) {
        println("Number: $num")
    }
}
    

This code creates a set where duplicate elements are automatically removed.

Maps

Maps store key-value pairs.

    
fun main() {
    val ages = mapOf("Alice" to 25, "Bob" to 30)
    for ((name, age) in ages) {
        println("$name is $age years old")
    }
}
    

The mapOf function creates a map, and the for loop iterates through key-value pairs.




Copyright © by Zafar Yasin. All rights reserved.