Control Flow

Control Flow

Codecastic
0

On this page

1. What are the basic control flow structures in Kotlin?

Basic control flow structures include if, else, when, for, and while.

2. How is the if expression different from the if statement in Kotlin?

The if expression returns a value, while the if statement does not. Example:

Copied

// Expression
val result = if (condition) "True" else "False"

// Statement
if (condition) {
// code
} else {
// code
}

3. Explain the usage of when in Kotlin and provide an example.

when is a versatile replacement for the traditional switch statement. It can be used as an expression or a statement. Example:

Copied

when (dayOfWeek) {
    1 -> println("Monday")
    2 -> println("Tuesday")
    // ... other cases
    else -> println("Unknown day"

4. How does Kotlin support the for loop, and what is the range notation used?

Kotlin uses ranges for for loops. Example:

Copied

for (i in 1..5) {
    println(i

5. Explain the while and do-while loops in Kotlin.

while executes a block of code as long as the condition is true, while do-while guarantees the block is executed at least once. Example:

Copied

// while
while (condition) {
    // code
}

// do-while
do {
    // code
} while (condition

6. What is the purpose of the break and continue statements in Kotlin loops?

break is used to exit a loop prematurely, and continue is used to skip the rest of the current iteration and move to the next one.

7. Explain the return statement in Kotlin and how it is used in control flow.

return is used to exit a function and, optionally, return a value. It can be used within loops or conditional statements.

8. How does Kotlin handle the when expression for multiple conditions?

when can be used with multiple conditions using commas or ranges. Example:

Copied

when (x) {
    in 1..10 -> println("In range 1 to 10")
    20, 30 -> println("Either 20 or 30")
    else -> println("Other cases"

9. What is the purpose of the is keyword in Kotlin, and how is it used?

is is used for type checking and type casting in Kotlin. Example:

Copied

val result = when (x) {
    is String -> "It's a String"
    is Int -> "It's an Int"
    else -> "Unknown type"

10. Explain the usage of labeled breaks and continues in Kotlin loops.

Labeled breaks and continues allow you to specify which loop to break or continue when dealing with nested loops. Example:

Copied

outer@ for (i in 1..5) {
    for (j in 1..5) {
        if (someCondition) {
            break@outer // break out of the outer loop

11. What is the purpose of the else branch in the when expression?

The else branch is executed when none of the other conditions in the when expression are satisfied. It is similar to the default case in a switch statement.

12. How does Kotlin support the in operator in control flow?

The in operator is used to check if a value is within a range or a collection. Example:

Copied

val x = 5
if (x in 1..10) {
    println("Value is in the range"

13. Explain the return keyword in the context of labeled returns.

Labeled returns allow you to specify which function to return from when dealing with nested functions. Example:

Copied

fun foo() {
    listOf(1, 2, 3, 4, 5).forEach {
        if (it == 3) return@forEach // return from the forEach lambda, not the foo function
        println(it

14. How does Kotlin handle the when expression without arguments?

The when expression without arguments acts similarly to an if-else chain, and each condition is evaluated in order until a match is found. Example:

Copied

val result = when {
    x > 0 -> "Positive"
    x < 0 -> "Negative"
    else -> "Zero"

15. What is the purpose of the if expression with else if branches in Kotlin?

The else if branches allow you to evaluate multiple conditions in sequence. Example:

Copied

val result = if (x > 0) "Positive" else if (x < 0) "Negative" else "Zero"

16. How is the when expression useful for enum classes in Kotlin?

when can be used to handle different cases of enum classes concisely. Example:

Copied

enum class Color { RED, GREEN, BLUE }

val color = Color.GREEN
val result = when (color) {
    Color.RED -> "It's red"
    Color.GREEN -> "It's green"
    Color.BLUE -> "It's blue"

17. Explain the use of the if expression in Kotlin for assigning values.

The if expression can be used to assign different values based on a condition in a concise manner. Example:

Copied

val result = if (condition) "Value when true" else "Value when false"

18. How does Kotlin support the when expression for smart casts?

Smart casts in when automatically cast the checked value to a more specific type within the block, eliminating the need for explicit casting. Example:

Copied

val result = when (value) {
    is String -> value.length // 'value' is automatically cast to String
    is Int -> value * 2        // 'value' is automatically cast to Int
    else -> 0

19. Explain the repeat function in Kotlin and how it differs from a regular loop.

The repeat function is a higher-order function that repeats a given block of code a specified number of times. It simplifies the syntax for simple loop iterations. Example:

Copied

repeat(3) {
    println("Hello"

20. How does Kotlin handle the when expression for multiple conditions with different results?

when can be used with multiple conditions, and each condition can have a different result. Example:

Copied

when {
    x > 0 -> println("Positive")
    x < 0 -> println("Negative")
    else -> println("Zero"

Last updated -

Share this page