I'm new in Scala world and want to know what is the difference between Expressions and Statements and Why if-else is used for expression, not statements. And if there is a way to use statement in if-else?
Everything in Scala is an Expression!
val aCondition = true
val aConditionValue = if(aCondition) 4 else 2
println(aConditionValue)
To prove this we can just use this Expression in println directly
println(if(aCondition) 4 else 2)
Note: Here If else expression is giving the value instead of setting/doing something based on condition.
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