If, elsif, else statements in Ruby - Time & Space Complexity
We want to understand how the time it takes to run if, elsif, else statements changes as the input changes.
Specifically, we ask: does adding more conditions make the program slower?
Analyze the time complexity of the following code snippet.
def check_number(num)
if num < 0
"Negative"
elsif num == 0
"Zero"
else
"Positive"
end
end
This code checks if a number is negative, zero, or positive and returns a string accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking conditions one by one.
- How many times: At most, it checks each condition once in order until one matches.
Each time the function runs, it checks conditions in order. The number of checks does not grow with input size but depends on how many conditions there are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks max |
| 100 | 3 checks max |
| 1000 | 3 checks max |
Pattern observation: The number of checks stays the same no matter how big the input number is.
Time Complexity: O(1)
This means the time to run the if, elsif, else statements stays constant no matter the input size.
[X] Wrong: "More input means more time because the code checks all conditions every time."
[OK] Correct: The code stops checking as soon as one condition matches, and the number of conditions is fixed, so input size does not affect time.
Understanding that simple condition checks run in constant time helps you explain how your code behaves efficiently, which is a useful skill in many programming tasks.
"What if we added a loop inside each condition that runs n times? How would the time complexity change?"
Practice
What does the elsif keyword do in Ruby?
Solution
Step 1: Understand the role of
Theelsifelsifkeyword is used to check another condition only if the previousiforelsifcondition was false.Step 2: Compare with other options
Options A, C, and D describe behaviors that do not matchelsif. It does not end the statement, nor repeats code, nor runs only if all previous conditions were true.Final Answer:
It checks another condition if the previousiforelsifwas false. -> Option CQuick Check:
elsif= check next condition [OK]
elsif checks next condition if previous false [OK]- Thinking
elsifends the if statement - Confusing
elsifwithelse - Assuming
elsifruns only if all previous are true
Which of the following is the correct syntax for an if statement with an else in Ruby?
?
Solution
Step 1: Identify Ruby syntax for
Ruby usesifandelseif condition, then code indented or on next line, and ends withend. Curly braces or colons are not used.Step 2: Check each option
if x > 10 puts "Big" else puts "Small" end matches Ruby syntax exactly. Options A, B, and C use syntax from other languages like colons (A), braces (B), or missingend(C).Final Answer:
if x > 10 puts "Big" else puts "Small" end -> Option AQuick Check:
Rubyifends withend[OK]
if always ends with end [OK]- Using braces {} instead of
end - Forgetting the
endkeyword - Using colons like Python
What will be the output of this Ruby code?
score = 75 if score >= 90 puts "Grade A" elsif score >= 80 puts "Grade B" elsif score >= 70 puts "Grade C" else puts "Grade F" end
Solution
Step 1: Check conditions in order
score is 75. First condition: score >= 90 is false. Second: score >= 80 is false. Third: score >= 70 is true.Step 2: Output for first true condition
Since third condition is true, it prints "Grade C" and skips the rest.Final Answer:
Grade C -> Option DQuick Check:
75 >= 70 is true, so output is Grade C [OK]
- Choosing Grade B or A without checking order
- Ignoring that only first true condition runs
- Confusing >= with >
Find the error in this Ruby code snippet:
number = 10 if number > 5 puts "Big" elsif number < 3 puts "Small" else puts "Medium"
Solution
Step 1: Check Ruby syntax completeness
Rubyifstatements must end withend. The code snippet is missingend.Step 2: Verify other parts
elsifis correct in Ruby.putscan be used without parentheses. Comparison operators are correct.Final Answer:
Missingendto close theifstatement. -> Option BQuick Check:
Everyifneedsend[OK]
if with end [OK]- Replacing
elsifwithelse if - Adding unnecessary parentheses to
puts - Changing correct comparison operators
You want to write a Ruby program that prints "Positive", "Negative", or "Zero" depending on the value of num. Which code correctly uses if, elsif, and else to do this?
Solution
Step 1: Understand the conditions needed
We want to print "Positive" if num > 0, "Negative" if num < 0, and "Zero" otherwise.Step 2: Check each option's logic
if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end matches the logic exactly with correctif,elsif, andelse. if num >= 0 puts "Positive" elsif num <= 0 puts "Negative" else puts "Zero" end wrongly treats zero as positive. if num > 0 puts "Positive" else if num < 0 puts "Negative" else puts "Zero" end uses invalidelse ifsyntax in Ruby. if num > 0 puts "Positive" elsif num > 0 puts "Negative" else puts "Zero" end repeats the same condition twice, which is incorrect.Final Answer:
if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end -> Option AQuick Check:
Correct conditions and syntax = if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end [OK]
elsif for second condition, else for default [OK]- Using
else ifinstead ofelsif - Repeating same condition in
elsif - Misclassifying zero as positive or negative
Ready to prove it?
Pass the test and this pattern moves unknown
