Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression statement in Kotlin

Tags:

kotlin

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?

like image 361
Alf Moh Avatar asked Jul 01 '17 08:07

Alf Moh


People also ask

What is expression function 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.

What is the difference between expression and statement in programming?

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.

What is when statement Kotlin?

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.


1 Answers

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.

like image 155
JB Nizet Avatar answered Oct 07 '22 21:10

JB Nizet