If in Java i++
is an expression and i++;
is an expression statement and semicolons(;) are optional in Kotlin, is i++
an expression or expression statement in Kotlin?
Kotlin single-expression function is a function where a single expression is assigned to the function, and the expression's evaluated value is returned when this function is called. The syntax of Kotlin Single-Expression Function is fun functionName(parameters) = expression.
In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.
In Kotlin, when replaces the switch operator of other languages like Java. A certain block of code needs to be executed when some condition is fulfilled. The argument of when expression compares with all the branches one by one until some match is found.
i++
is an expression, because it has a value, that you can assign to a variable, pass as an argument to a method, etc.
If you just have i++
, and don't do anything with its value, you use it as a statement, i.e. only for the side-effects it has, and not for its value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With