Kotlin Expression, Statement and Block (original) (raw)
Last Updated : 10 May, 2025
Every Kotlin program is made up of parts that either calculate values, called **expressions, or carry out actions, known as **statements. These parts can be organized into sections called **blocks.
Table of Content
Kotlin Expression
An expression in Kotlin is made up of variables, operators, method calls, and so on, which combine to produce a single value. Just like in other programming languages, expressions are essential building blocks used to create new values. Sometimes expressions are used to assign values to variables. It’s important to note that expressions can also contain other expressions.
Here are some examples of what is **not considered an expression:
- A variable declaration, like **var a = 100
- Assigning a value, such as **b = 15
- A class declaration, like **class XYZ {....}
**Note: In Kotlin, every function returns a value, even if it's just "Unit," which means every function is considered an expression.
**Example:
Kotlin `
fun sumOf(a:Int,b:Int): Int{ return a+b }
fun main(args: Array){ val a = 10 val b = 5 var sum = sumOf(a,b) var mul = a * b println(sum) println(mul) }
`
**Output:
15
50
When we say **a * b and **sumOf(a, b), both are expressions that return an integer value. The **sumOf() function adds two numbers together.
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. Therefore, 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:
**Example:
Kotlin `
fun main(args: Array){ 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 aand{a} and aand{b} is $max1 " ) println("The maximum of cand{c} and cand{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 a unit of code in any programming language that performs an action. A program is created by a series of these statements. In Java, a statement always ends with a semicolon, but in Kotlin, using a semicolon is optional.
Examples of statements include:
- 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:
You can write more than one statement on a single line.
**Example:
Kotlin `
fun main(args: Array){ val sum: Int sum = 100
// single statement
println(sum)
// Multiple statements
println("Hello");println("Geeks!")
}
`
**Output:
100
Hello
Geeks!
Kotlin Block
A block is a section of code surrounded by curly braces `{...}`. A block can have one or more statements and usually includes variable declarations. Blocks can also contain nested blocks. Every function has its block, and the main function includes a block as well.
**Example:
Kotlin `
// Start of main block or outer block
fun main(args: Array) {
val array = intArrayOf(2, 4, 6, 8)
// Start of inner block
for (element in array) {
println(element)
}
// End of inner block
}
// End of main block
`
**Output:
2
4
6
8
**Scope of variable in nested blocks:
Variables declared at the start of a block can be used throughout that block and any blocks nested within it. However, if a variable with the same name is declared in an inner block, that new variable will be used inside that inner block. When the inner block ends, the original variable from the outer block becomes visible again. This shows that variables have nested scopes.
Assignment
In this assignment, we will explore the concepts of expressions, statements, and blocks in Kotlin. Below is a Kotlin program that demonstrates which parts are expressions, which are statements, and which are blocks.
**Program:
Kotlin `
fun main() {
val num1 = 5 + 3
val num2 = 10
val bigger = if (num1 > num2) num1 else num2
if (bigger == num1) {
println("num1 is bigger")
} else {
println("num2 is bigger")
}
println("num1 = $num1")
println("num2 = $num2")
println("bigger = $bigger")
}
`
**Solution:
fun main() {
// Expression
val num1 = 5 + 3
// Statement
val num2 = 10
// if Expression
val bigger = if (num1 > num2) num1 else num2
// Block
if (bigger == num1) {
println("num1 is bigger")
} else {
println("num2 is bigger")
}
// Final output
println("num1 = $num1")
println("num2 = $num2")
println("bigger = $bigger")
}