If-else expression assignment in Kotlin - Time & Space Complexity
Let's see how the time it takes to run code changes when we use an if-else expression to assign a value.
We want to know how the program's steps grow as input changes.
Analyze the time complexity of the following code snippet.
val number = 10
val result = if (number % 2 == 0) {
"Even"
} else {
"Odd"
}
println(result)
This code checks if a number is even or odd and assigns a string based on that.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per run.
The program does the same steps no matter what number you use.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of steps stays the same even if the input changes.
Time Complexity: O(1)
This means the program takes the same amount of time no matter the input size.
[X] Wrong: "If-else checks take longer as the number gets bigger."
[OK] Correct: The check only looks at the number once, so size doesn't affect time.
Understanding simple if-else assignments helps you explain how decisions in code affect speed, a key skill in many coding tasks.
"What if we replaced the if-else with a loop that checks every number up to 'number'? How would the time complexity change?"
Practice
val result = if (score > 50) "Pass" else "Fail"Solution
Step 1: Understand the if-else expression
The code uses if-else as an expression to assign a value to result based on the condition score > 50.Step 2: Evaluate the condition and assignment
If score is greater than 50, result gets "Pass"; otherwise, it gets "Fail".Final Answer:
Assigns "Pass" to result if score is greater than 50, otherwise "Fail" -> Option AQuick Check:
If-else expression assigns value based on condition [OK]
- Forgetting the else part causes a syntax error
- Thinking if-else is only for flow, not value assignment
- Confusing condition with assignment
Solution
Step 1: Review Kotlin if-else expression syntax
The correct syntax uses if(condition) value1 else value2 without extra keywords or misplaced braces.Step 2: Compare options with correct syntax
val x = if (a > b) 10 else 20 matches the correct syntax: val x = if (a > b) 10 else 20.Final Answer:
val x = if (a > b) 10 else 20 -> Option AQuick Check:
Correct if-else assignment syntax is val x = if (cond) val1 else val2 [OK]
- Using 'then' keyword which Kotlin does not have
- Misplacing equals signs around else
- Adding extra else after braces
val age = 18 val status = if (age >= 18) "Adult" else "Minor" println(status)
Solution
Step 1: Check the condition age >= 18
Since age is 18, the condition age >= 18 is true.Step 2: Determine the assigned value to status
Because the condition is true, status is assigned "Adult".Final Answer:
Adult -> Option BQuick Check:
Condition true assigns "Adult" [OK]
- Assuming >= 18 is false for 18
- Printing age instead of status
- Confusing syntax causing compile error
val x = if (num > 0) "Positive"
Solution
Step 1: Check if-else expression completeness
An if-else expression used for assignment must have both if and else branches.Step 2: Identify missing else branch
The code only has if (num > 0) "Positive" but no else part, causing a syntax error.Final Answer:
Missing else branch in if-else expression -> Option CQuick Check:
If-else expression must include else [OK]
- Omitting else branch
- Thinking if alone can assign value
- Ignoring compiler error messages
Solution
Step 1: Understand nested if-else expression syntax
Kotlin allows nested if-else expressions to assign values based on multiple conditions.Step 2: Check each option for correct syntax and logic
val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C" correctly nests if-else expressions with proper syntax and covers all cases.Final Answer:
val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C" -> Option DQuick Check:
Nested if-else expressions assign multiple conditions [OK]
- Missing parentheses in nested if
- Incorrect else placement
- Adding extra else after braces
Ready to prove it?
Pass the test and this pattern moves unknown
