Overview - If as an expression returning value
What is it?
In Kotlin, the 'if' statement can be used as an expression that returns a value. This means you can assign the result of an 'if' directly to a variable. Unlike many languages where 'if' is only a control flow statement, Kotlin treats it as an expression that produces a result based on the condition.
Why it matters
Using 'if' as an expression makes code shorter and clearer by combining decision-making and value assignment in one step. Without this, you would need extra lines to declare variables and assign values inside each branch, making code longer and harder to read.
Where it fits
Before learning this, you should understand basic Kotlin syntax, variables, and control flow statements. After this, you can explore more advanced expressions like 'when' expressions and lambda functions.