While loops in MATLAB - Time & Space Complexity
We want to understand how the time a while loop takes changes as the input grows.
How many times does the loop run when input size changes?
Analyze the time complexity of the following code snippet.
n = 1;
while n <= N
disp(n);
n = n + 1;
end
This code prints numbers from 1 up to N using a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs once for each number from 1 to N, so N times.
As N gets bigger, the loop runs more times, growing steadily.
| Input Size (N) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with N.
Time Complexity: O(N)
This means the time grows in a straight line as the input size increases.
[X] Wrong: "The while loop runs forever or a fixed number of times regardless of N."
[OK] Correct: The loop runs exactly N times because it counts from 1 to N, so it depends directly on input size.
Understanding how loops grow with input helps you explain your code clearly and shows you can think about efficiency.
"What if we changed the loop to increase n by 2 each time? How would the time complexity change?"
Practice
while loop do in MATLAB?Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition remains true.Step 2: Compare options with definition
Only Repeats a block of code as long as a condition is true correctly describes this behavior.Final Answer:
Repeats a block of code as long as a condition is true -> Option AQuick Check:
While loop = repeat while true [OK]
- Thinking while loops run a fixed number of times
- Confusing while with if statement
- Believing while loops run only once
while loop in MATLAB?Solution
Step 1: Recall MATLAB while loop syntax
MATLAB uses 'while condition' followed by statements and ends with 'end'.Step 2: Check each option
while condition statements end matches MATLAB syntax; others use syntax from other languages.Final Answer:
while condition\n statements\nend -> Option AQuick Check:
MATLAB uses 'end' to close loops [OK]
- Using braces {} instead of 'end'
- Adding colons after condition
- Using 'do' keyword
i = 1; while i <= 3 disp(i) i = i + 1; end
Solution
Step 1: Analyze loop condition and increments
The loop runs while i is less than or equal to 3, starting at 1 and increasing by 1 each time.Step 2: Trace output values
Values displayed are 1, then 2, then 3. When i becomes 4, condition fails and loop stops.Final Answer:
1 2 3 -> Option BQuick Check:
Loop prints 1 to 3 [OK]
- Forgetting to increment i causing infinite loop
- Expecting output to include 4
- Confusing loop condition with < instead of <=
count = 5; while count > 0 disp(count) end
Solution
Step 1: Check loop condition and body
The loop condition is 'count > 0', but inside the loop count is never changed.Step 2: Understand effect of missing update
Without changing count, the condition stays true forever, causing an infinite loop.Final Answer:
Missing increment or decrement inside the loop -> Option CQuick Check:
Update loop variable to avoid infinite loop [OK]
- Forgetting to change loop variable
- Assuming disp causes error
- Thinking syntax is wrong
while loop that sums numbers from 1 to 10 and stores the result in total. Which code correctly does this?Solution
Step 1: Understand the goal and loop condition
We want to add numbers 1 through 10 using a while loop that stops when i > 10.Step 2: Check each option for correctness
total = 0; i = 1; while i <= 10 total = total + i; i = i + 1; end initializes total and i, adds i to total, increments i, and stops at 10 correctly. total = 0; i = 1; while i < 10 total = total + i; end misses increment causing infinite loop. total = 0; for i = 1:10 total = total + i; end uses for loop, not while. total = 0; i = 1; while i <= 10 total = total + i; i = i - 1; end decrements i causing infinite loop.Final Answer:
total = 0;\ni = 1;\nwhile i <= 10\n total = total + i;\n i = i + 1;\nend -> Option DQuick Check:
Increment i to sum 1 to 10 [OK]
- Forgetting to increment i
- Using for loop instead of while
- Decrementing i causing infinite loop
Ready to prove it?
Pass the test and this pattern moves unknown
