In this tutorial you will learn about the Kotlin Expression & Statement and its application with practical example.
Kotlin Expression
An expressions is consist of variables, operators, literals and method calls that evaluates to a value.
Example:-
1 2 3 4 5 6 |
fun main(args: Array<String>) { val num1 = 15 val res: Int result= num1 + 20 println("$result") } |
Here, num1+20 is an expression, the value of the expression is assigned to variable result.
Kotlin Statement
Kotlin statement is an executable statements, that tell the computer to perform a specification action.Program statement can be an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it also includes comments.
Example:-
1 |
val result= 25 + 10 |
Here, 25 + 10 is an expression that returns 35, and val result= 25 + 10 is a statement.
Kotlin Block
In Kotlin, block is a group of statement(s) that is enclosed in curly braces { }.
Example:-
1 2 3 4 5 6 7 |
fun main(args: Array<String>) { // main function block val flag = true if (flag == true) { // start of if block print("Hello ") print("World!") } // end of if block } |