Statement or Expression
What is the difference between a statement and expression?
A major difference is an expression has an actual value while a statement does NOT have one.
Title | Valued | Examples |
---|---|---|
Statement | does not have it's own value | assignments while do-while |
Expression | has a value | when if-else try-catch |
What difference does it make?
You can assign an expression to a variable but you can't do that for statements. For example :
val person = if (true) "Koltiner" else "Droid" // valid
var message = "Hello"
val value = message = "Bonjour" //Assignments are not expressions, and only expressions are allowed in this context
Why is this important to know?
Understanding the difference will save you from creating unwanted variables which can lead to errors and also help minimise using more words than needed (verbosity). Case and point is when an if
expression as used to assign a value to a variable
val message: String
if (someCondition) message = "Hello" else message = "Bonjour"
println(message)
In the example above we have repeated the variable
message
three times, this is using more words than needed. By understanding the difference between statements and expressions we can refactior the above code to remove redundancy.
val message = if (someCondition) "Hello" else "Bonjour"
println(message)
This also works for
when
&try-catch
val message = when(someCondition){
true -> "Hello"
false -> "Bonjour"
}
val price = try {
"100o".toInt() // think of this as converting a variable to int
} catch (e: Exception){
0
}
Where else can this work?
This is extremely useful when working with a function that has an expression body.
fun getMessage(someCondition: Boolean) = if (someCondition) "Hello" else "Bonjour"
This is a great way to express your functions in a clear and concise manner.
You can find more usages in other bits, keep on browsing and add a reaction below or comment 😉 😉...