Skip to content
Related Articles

Related Articles

Improve Article
Kotlin Expression, Statement and Block
  • Last Updated : 13 May, 2019

Kotlin Expression –

An expression consists of variables, operators, methods calls etc that produce a single value. Like other language, Kotlin expression is building blocks of any program that are usually created to produce new value. Sometimes, it can be used to assign a value to a variable in a program.It is to be noted that an expression can contain another expression.

  • A variable declaration can not be an expression (var a = 100)
  • Assigning a value is not an expression (b = 15)
  • A class declaration is not an expression (class XYZ {….})

Note: In Kotlin every function returns a value atleast Unit, so every function is an expression.

Let’s take an example:




fun sumOf(a:Int,b:Int): Int{
    return a+b
}
  
fun main(args: Array<String>){
    val a = 10
    val b = 5
    var sum = sumOf(a,b)
    var mul = a * b
    println(sum)
    println(mul)
}

Output:

15
50

Here, a * b and sumof(a, b) both are expressions and return integer value. sumOf() is a function and returns the sum of two parameters passed to it.



Kotlin if expression –

In Java, if is a statement but, in Kotlin if is an expression. It is called an expression because it compares the values of a and b and returns the maximum value. Therfore, in Kotlin there is no ternary operator (a>b)?a:b because it is replaced by the if expression.

if(condition) condition met! else condition not met!

Let’s take an example to return the maximum value among two variables:




fun main(args: Array<String>){
    val a = 1000
    val b = 999
    var c = 1122
    var max1 = if(a > b) a else b
    var max2 = if(c > a) c else a
    println("The maximum of ${a} and ${b} is $max1 " )
    println("The maximum of ${c} and ${a} is $max2 " )
}

Output:

The maximum of 1000 and 999 is 1000 
The maximum of 1122 and 1000 is 1122 

Kotlin Statement –

A statement is the syntactic unit of any programming language that expresses some action to be carried out. A program is formed by the sequence of one or more statements. In Java, a statement always ends with a semicolon but, in Koltin semicolon(;) is optional.

  • Declaration of a variable is a statement.
    val marks = 90
    var grade = 'A'
    
  • Assigning a value to a variable is also a statement.
    var sum = 10 + 20        // it is a statement
    

    Here, 10 + 20 is an expression but var sum = 10 + 20 is a statement.

    Multiple Statements:
    Multiple statements are the statements when you write more than one statement in a single line.

    For example:




    fun main(args: Array<String>){
        val sum: Int
        sum = 100
        println(sum)                             // single statement
        println("Hello");println("Geeks!")       // Mutilple statements
    }

    Output:

    100
    Hello
    Geeks!
    

    Kotlin Block –

    A block is a section of software code enclosed with curly braces ({…}). A block can consist of one or more statements, preceded by the declarations of variables. A block contains one or more blocks nested within it. Every function has its own block and main function also contains a block.

    For example:




    fun main(args: Array<String>) {              //start of main block or outer block
         val array = intArrayOf(2, 4, 6, 8)
         for (element in array) {                // start of inner block
            println(element)
         }                                       // end of inner block
     }                                           // end of main block
  • Output:

    2
    4
    6
    8
    

    Scope of variable in nested blocks:
    The variables declared at the head of the block are visible throughout the block and any nested blocks, unless a variable with the same name declared at the head in the inner block. When a new declaration is effective throughout the inner block, the outer declaration becomes effective again at the end of the inner block. So, we can say that variables have nested scopes.




    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :