for index = startValue:endValue
% code to repeat
end
This repeats the code for each value of index from startValue to endValue.
Click to reveal answer
beginner
How does a for loop in MATLAB differ from a while loop?
A for loop runs a fixed number of times, determined before the loop starts.
A while loop runs as long as a condition is true, which can change during execution.
Click to reveal answer
intermediate
What happens if the loop variable in a MATLAB for loop is a vector?
The loop runs once for each element in the vector, assigning the loop variable to that element each time. Example:
for i = [2, 4, 6]
disp(i)
end
prints 2, then 4, then 6.
Click to reveal answer
intermediate
Can you modify the loop variable inside a MATLAB for loop? What happens?
Modifying the loop variable inside the loop does not affect the next iteration. MATLAB sets the loop variable to the next value in the sequence at the start of each iteration regardless of changes inside the loop.
Click to reveal answer
beginner
How do you exit a for loop early in MATLAB?
Use the break statement inside the loop. When MATLAB encounters break, it stops the loop immediately and continues with the code after the loop.
Click to reveal answer
What does this MATLAB code print?
for i = 1:3
disp(i)
end
AError
B3 2 1
C1 1 1
D1 2 3
✗ Incorrect
The loop runs from 1 to 3, printing each number in order.
In MATLAB, what happens if you write:
for i = [5 10 15]
disp(i)
end
AIt prints 5, then 10, then 15
BIt prints 1 to 15
CIt prints only 15
DIt causes an error
✗ Incorrect
The loop variable takes each value in the vector in order.
What is the purpose of the break statement in a MATLAB for loop?
ATo skip the current iteration
BTo stop the loop immediately
CTo restart the loop
DTo pause the loop
✗ Incorrect
break stops the loop right away and continues after it.
If you change the loop variable inside a MATLAB for loop, what happens in the next iteration?
AThe loop variable resets to the next value in the sequence
BThe loop stops
CThe changed value is used
DAn error occurs
✗ Incorrect
MATLAB resets the loop variable to the next value in the sequence regardless of changes.
Which of these is a valid for loop header in MATLAB?
Afor i = 1 to 5
Bfor i in 1:5
Cfor i = 1:5
Dfor (i=1; i<=5; i++)
✗ Incorrect
MATLAB uses for i = 1:5 syntax for loops.
Explain how a for loop works in MATLAB and give an example.
Think about how the loop variable changes each time.
You got /4 concepts.
Describe what happens if you modify the loop variable inside a MATLAB for loop.
Consider how MATLAB controls the loop variable each iteration.
You got /3 concepts.
Practice
(1/5)
1. What does a for loop do in MATLAB?
easy
A. Stops the program immediately
B. Runs commands only once
C. Creates a new variable
D. Repeats a set of commands a fixed number of times
Solution
Step 1: Understand the purpose of a for loop
A for loop is designed to repeat commands multiple times based on a range or list.
Step 2: Compare options with the definition
Only Repeats a set of commands a fixed number of times correctly describes repeating commands a fixed number of times.
Final Answer:
Repeats a set of commands a fixed number of times -> Option D
Quick Check:
For loop = repeat commands [OK]
Hint: For loops repeat commands multiple times [OK]
Common Mistakes:
Thinking for loops run only once
Confusing for loops with if statements
Believing for loops stop the program
2. Which of the following is the correct syntax for a for loop in MATLAB?
easy
A. for i = 1:5
disp(i)
end
B. for (i = 1; i <= 5; i++)
disp(i)
end
C. loop i from 1 to 5
disp(i)
end
D. for i in range(1,5):
disp(i)
Solution
Step 1: Identify MATLAB for loop syntax
MATLAB uses 'for variable = start:end' and ends with 'end'.
Step 2: Check each option
for i = 1:5
disp(i)
end matches MATLAB syntax. Options B, C, and D use other languages' syntax.
Final Answer:
for i = 1:5\n disp(i)\nend -> Option A
Quick Check:
MATLAB for loop = for i = 1:5 ... end [OK]
Hint: MATLAB for loops use 'for var = start:end' and end with 'end' [OK]
Common Mistakes:
Using parentheses like C or JavaScript
Using Python or other language syntax
Forgetting the 'end' keyword
3. What is the output of this MATLAB code?
sum = 0;
for i = 1:3
sum = sum + i;
end
disp(sum)
medium
A. 6
B. 3
C. 0
D. Error
Solution
Step 1: Trace the loop iterations
The loop runs i = 1, 2, 3. sum starts at 0.
Step 2: Calculate sum after each iteration
sum = 0+1=1, then 1+2=3, then 3+3=6.
Final Answer:
6 -> Option A
Quick Check:
1+2+3 = 6 [OK]
Hint: Add numbers 1 to 3 inside loop [OK]
Common Mistakes:
Forgetting to initialize sum
Adding outside the loop
Confusing loop range
4. Identify the error in this MATLAB for loop:
for i = 1:4
disp(i)
i = i + 1;
end
medium
A. Missing 'end' keyword
B. Modifying loop variable inside the loop
C. Incorrect loop range syntax
D. disp function is invalid
Solution
Step 1: Review loop variable usage
The loop variable 'i' is controlled by MATLAB and should not be changed inside the loop.
Step 2: Identify the problem
Changing 'i' inside the loop causes unexpected behavior or errors.
Final Answer:
Modifying loop variable inside the loop -> Option B
Quick Check:
Do not change loop variable inside for loop [OK]
Hint: Never change the loop variable inside the loop [OK]
Common Mistakes:
Changing loop variable inside loop body
Forgetting 'end' keyword (not the case here)
Misunderstanding disp function
5. How can you use a for loop in MATLAB to create a vector v containing squares of numbers from 1 to 5?
hard
A. v = [];
for i = 1:5
v = v + i^2;
end
B. for i = 1:5
v(i) = i*2;
end
C. v = zeros(1,5);
for i = 1:5
v(i) = i^2;
end
D. v = 1:5;
for i = 1:5
v(i) = i + 2;
end
Solution
Step 1: Initialize vector to store results
Pre-allocate vector v with zeros to hold 5 elements.
Step 2: Use for loop to assign squares
For each i from 1 to 5, assign v(i) = i squared.
Final Answer:
v = zeros(1,5);\nfor i = 1:5\n v(i) = i^2;\nend -> Option C
Quick Check:
Pre-allocate vector and assign squares in loop [OK]