Kotlin statement expression: In this tutorial, you will learn about Kotlin expressions, Kotlin statements, differences among expressions and statements, and Kotlin blocks.
Kotlin Expressions
Like other programming languages, Kotlin expression is the building block of any kotlin program that is generally created to produce new value. In some cases, it is used to simply assign a value to a variable. Kotlin expression is created using values, variables, operators, or technique calls. At long last, expressions results in a single value.
Expressions comprise of variables, operators and so forth that assesses to a single value.
Let’s take an example,
val score: Int score = 90 + 25
Here, 90 + 25 is an expression that returns Int value.
In Kotlin, if is an articulation dissimilar to Java (In Java, if is a statement). For instance,
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 articulation. At that point value of the articulation is appointed to max variable in the above program.
Kotlin Statements
Statements are all that make up a total unit of execution. For instance,
val score = 90 + 25
Here, 90 + 25 is an expression that returns 115, and val score = 9*5; is a statement.
Expressions are part of statements.
some examples:
println("Howdy")
var a = 5 ++a
max = if (a > b) a else b
Kotlin Blocks
A block is a group of statements (zero or more) that is encased in curly braces { }. For instance,
fun main(args: Array<String>) { // main function block val flag = true if (flag == true) { // start of if block print("Hey ") print("sohail!") } // end of if block } // end of main function block
There are two statements print(“Hey “) and print(” sohail!”) inside if branch block.
print("Hey ") print("sohail!")
Additionally, the main() work likewise has a block body.
val flag = true if (flag == true) { // start of block print("Hey ") print("sohail!") } // end of block
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.