English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Kotlin expressions, Kotlin statements, the difference between expressions and statements in Kotlin, and Kotlin blocks.
An expression is calculated to be a single value.Variables,Operatorsconstitute.
Let's take an example
val score: Int score = 90 + 25
Here 90 + 25is an expression that returns an Int value.
In Kotlin, if is an expression, unlike Java (in Java, if is a statement). For example
fun main(args: Array<String>) { val a = 12 val b = 13 val max: Int max = if (a > b) a else b println("$max") }
Here, if (a > b) a else b is an expression. Then the value of the expression is assigned to the max variable in the above program.
A statement is all the content that makes up a complete execution unit. For example,
val score = 90 + 25
Here,90 + 25 is a return115The expression, and val score = 9*5; is a statement.
An expression is a part of a statement.
Some examples:
println("Howdy")
var a = 5 ++a
max = if (a > b) a else b
A block is a group of statements (zero or more) enclosed in curly braces { }. For example,
fun main(args: Array<String>) { // main function block val flag = true if (flag == true) { //Start of if block print("Hey ") print("jude!") } //End of if block } // End of main function block
These are two statements inside the if branch block: print("Hey ") and print(" jude!").
print("Hey ") print("jude!")
Similarly, the main() function also has a block body.
val flag = true if (flag == true) { //Start Block print("Hey ") print("jude!") } //End Block