Bird
Raised Fist0
MATLABdata~10 mins

For loops in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loops
Initialize loop variable i = start
↓
Check: i <= end?
No→EXIT loop
Yes↓
Execute loop body
↓
Update i = i + step
↩Back to Check
The loop starts by setting a variable, checks if it meets the condition, runs the code inside if yes, then updates the variable and repeats until the condition is false.
Execution Sample
MATLAB
for i = 1:3
    disp(i)
end
This loop prints numbers 1, 2, and 3 one by one.
Execution Table
Iterationi valueCondition (i <= 3)ActionOutput
11trueDisplay i1
22trueDisplay i2
33trueDisplay i3
44falseExit loop
💡 i reaches 4, condition 4 <= 3 is false, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
i12344
Key Moments - 3 Insights
Why does the loop stop after i = 3 and not continue?
Because the condition i <= 3 becomes false when i is 4, as shown in the last row of the execution table where the loop exits.
Does the loop body run when the condition is false?
No, the loop body only runs when the condition is true. The execution table shows the loop exits immediately when the condition is false at iteration 4.
What happens to the variable i after the loop ends?
The variable i becomes 4 after the last update, which caused the loop to stop, as tracked in the variable tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i during the 2nd iteration?
A2
B3
C1
D4
💡 Hint
Check the 'i value' column in the execution table at iteration 2.
At which iteration does the loop condition become false?
A3
B1
C4
D2
💡 Hint
Look at the 'Condition' column in the execution table where it changes to false.
If the loop was changed to for i = 1:4, how many times would the loop body run?
A3 times
B4 times
C5 times
D1 time
💡 Hint
The loop runs once for each value of i from start to end, inclusive, as shown in the variable tracker.
Concept Snapshot
for i = start:end
    % code to repeat
end
- Loop runs from start to end values
- Executes code inside for each i
- Stops when i exceeds end
- i updates automatically each iteration
Full Transcript
This visual shows how a for loop in MATLAB works. The loop starts by setting i to 1. It checks if i is less than or equal to 3. If yes, it runs the code inside, here displaying i. Then i increases by 1. This repeats until i becomes 4, which fails the condition, so the loop stops. The variable tracker shows i's value at each step. The key moments clarify common confusions like why the loop stops and when the body runs. The quiz tests understanding of i's values and loop stopping conditions.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Repeats a set of commands a fixed number of times -> Option D
  4. 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

  1. Step 1: Identify MATLAB for loop syntax

    MATLAB uses 'for variable = start:end' and ends with 'end'.
  2. Step 2: Check each option

    for i = 1:5 disp(i) end matches MATLAB syntax. Options B, C, and D use other languages' syntax.
  3. Final Answer:

    for i = 1:5\n disp(i)\nend -> Option A
  4. 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

  1. Step 1: Trace the loop iterations

    The loop runs i = 1, 2, 3. sum starts at 0.
  2. Step 2: Calculate sum after each iteration

    sum = 0+1=1, then 1+2=3, then 3+3=6.
  3. Final Answer:

    6 -> Option A
  4. 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

  1. Step 1: Review loop variable usage

    The loop variable 'i' is controlled by MATLAB and should not be changed inside the loop.
  2. Step 2: Identify the problem

    Changing 'i' inside the loop causes unexpected behavior or errors.
  3. Final Answer:

    Modifying loop variable inside the loop -> Option B
  4. 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

  1. Step 1: Initialize vector to store results

    Pre-allocate vector v with zeros to hold 5 elements.
  2. Step 2: Use for loop to assign squares

    For each i from 1 to 5, assign v(i) = i squared.
  3. Final Answer:

    v = zeros(1,5);\nfor i = 1:5\n v(i) = i^2;\nend -> Option C
  4. Quick Check:

    Pre-allocate vector and assign squares in loop [OK]
Hint: Pre-allocate vector, assign squares inside loop [OK]
Common Mistakes:
  • Not pre-allocating vector before loop
  • Adding instead of assigning inside loop
  • Using wrong operation for squares
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard