0
0
Kotlinprogramming~10 mins

If as an expression returning value in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If as an expression returning value
Evaluate condition
Condition true?
NoEvaluate else branch
Return else value
Evaluate then branch
Return then value
Use returned value
The if expression evaluates a condition, then returns the value of the 'then' or 'else' branch accordingly.
Execution Sample
Kotlin
val max = if (a > b) a else b
println(max)
This code assigns the greater of a and b to max using if as an expression, then prints max.
Execution Table
StepCondition (a > b)Branch TakenReturned ValueOutput
15 > 3then5
2N/AN/A55
32 > 4else4
4N/AN/A44
53 > 3else3
6N/AN/A33
💡 Execution stops after returning value from if expression and printing it.
Variable Tracker
VariableStartAfter 1After 2After 3Final
a55233
b33433
maxunassigned5433
Key Moments - 3 Insights
Why does the if expression return a value instead of just running code?
Because in Kotlin, if is an expression that evaluates to the value of the branch taken, as shown in execution_table rows 1 and 3.
What happens if the condition is false and there is no else branch?
The code will not compile because if used as an expression must have an else branch to return a value, as implied by the need to return a value in all cases.
Can the returned value from if be assigned directly to a variable?
Yes, as shown in execution_table where max is assigned the returned value from the if expression.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the returned value when a=5 and b=3 at step 1?
A8
B5
C3
Dfalse
💡 Hint
Check the 'Returned Value' column at step 1 where condition is true.
At which step does the condition evaluate to false and the else branch is taken?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at the 'Condition' and 'Branch Taken' columns in execution_table.
If we remove the else branch, what will happen when compiling this code?
AIt will cause a compile error
BIt will compile and run normally
CIt will return null
DIt will return zero
💡 Hint
Refer to key_moments about the necessity of else branch for if expressions.
Concept Snapshot
if expression syntax:
val result = if (condition) value1 else value2

- Evaluates condition
- Returns value1 if true
- Returns value2 if false
- Must have else branch when used as expression
- Can assign returned value directly to variable
Full Transcript
In Kotlin, if can be used as an expression that returns a value. The program checks the condition; if true, it evaluates and returns the 'then' branch value; if false, it evaluates and returns the 'else' branch value. This returned value can be assigned directly to a variable. The execution table shows examples with different values of a and b, demonstrating which branch is taken and what value is returned. Remember, when using if as an expression, you must include an else branch to cover all cases. This makes your code concise and expressive.