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:
Kotlin
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. 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:
Kotlin
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 Kotlin 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:
Kotlin
fun main(args: Array<String>){
val sum: Int
sum = 100
println(sum)
println( "Hello" );println( "Geeks!" )
}
|
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:
Kotlin
fun main(args: Array<String>) {
val array = intArrayOf( 2 , 4 , 6 , 8 )
for (element in array) {
println(element)
}
}
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Jul, 2022
Like Article
Save Article