Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin Expression, Statement and Block

In Kotlin, as in many programming languages, you'll encounter the terms "expression," "statement," and "block." Understanding the difference between these is crucial as you learn to read and write Kotlin code. Let's dive into what each of these terms means:

1. Expression:

An expression in Kotlin is a combination of variables, values, and operators that is evaluated to produce a value. Functions can also be part of expressions, provided they return a value.

  • Every expression has a value.
  • Every expression has a type.

Examples:

val sum = 3 + 5   // 3 + 5 is an expression that evaluates to 8

val result = if (sum > 10) "Greater" else "Smaller"  // The entire if-else construct here is an expression.

In the second example, the if-else construct in Kotlin acts as an expression because it returns a value.

2. Statement:

A statement in Kotlin is a unit of code that represents an action or command to be executed. In contrast to expressions, statements do not produce values. In fact, in Kotlin, most things that are statements in other languages (like assignments or loops) are expressions. However, not every expression can be considered a statement.

  • A statement doesn't return a value.
  • A statement represents an action or command.

Examples:

val number = 5   // This is a declaration statement.

println("Hello, World!")  // This is a function invocation statement.

In Kotlin, fewer constructs are purely statements compared to languages like Java because many Kotlin constructs can return values (and are thus expressions).

3. Block:

A block in Kotlin is a group of statements (and possibly expressions) enclosed in curly braces { }. A block is itself an expression if it returns a value, which is the value of the last expression in the block.

Example:

val maxNumber = if (a > b) {
    println("Choosing 'a'")
    a   // Last expression's value ('a') is the value of the whole block.
} else {
    println("Choosing 'b'")
    b   // Last expression's value ('b') is the value of the whole block.
}

In this example, the blocks inside the if and else branches are expressions because they return a value.

In Summary:

  • An expression evaluates to a value and has a type.
  • A statement performs an action and does not produce a value.
  • A block is a group of statements and expressions, and it can act as an expression itself if it returns a value.

Understanding these fundamental concepts can help you read and write Kotlin code more effectively and also helps in grasping more complex Kotlin features built on these basics.

  1. Examples of Kotlin expressions:

    • Expressions produce a result.
    val sum = 3 + 5
    
  2. Kotlin single-line vs multi-line expressions:

    • Single-line expressions can be written on one line.
    val sum = 3 + 5
    
    • Multi-line expressions span multiple lines.
    val result = run {
        val a = 10
        val b = 20
        a + b
    }
    
  3. Kotlin conditional expressions:

    • Conditional expressions return a value based on a condition.
    val result = if (condition) "True" else "False"
    
  4. Kotlin return statements:

    • return is used to exit a function and provide a result.
    fun add(a: Int, b: Int): Int {
        return a + b
    }
    
  5. Blocks and scoping in Kotlin:

    • Blocks define a scope, and variables declared inside are limited to that scope.
    fun example() {
        val a = 10
        if (condition) {
            val b = 20
            println(a + b)
        }
        // b is not accessible here
    }
    
  6. How to use expressions in Kotlin functions:

    • Functions can return expressions.
    fun multiply(a: Int, b: Int): Int = a * b
    
  7. Kotlin when expression usage:

    • The when expression is a powerful switch-like construct.
    val result = when (value) {
        1 -> "One"
        2 -> "Two"
        else -> "Other"
    }
    
  8. Kotlin if-else expressions:

    • if-else can be used as an expression.
    val result = if (condition) "True" else "False"
    
  9. Statements in Kotlin loops:

    • Loop statements perform iterations.
    for (i in 1..5) {
        println(i)
    }
    
  10. Kotlin lambda expressions as statements:

    • Lambda expressions can be used as statements.
    val printMessage: () -> Unit = { println("Hello") }
    
  11. Use of semicolons in Kotlin:

    • Semicolons are optional in Kotlin. They are used to separate statements on the same line.
    val a = 5; val b = 10