0
0
MATLABdata~20 mins

For loops in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple for loop with indexing
What is the output of the following MATLAB code?
MATLAB
sumVal = 0;
for i = 1:4
    sumVal = sumVal + i;
end
sumVal
A10
B24
C0
DError: Undefined variable 'sumVal'
Attempts:
2 left
💡 Hint
Think about adding numbers from 1 to 4 step by step.
Predict Output
intermediate
2:00remaining
For loop with decrementing index
What will be the value of variable 'result' after running this code?
MATLAB
result = 1;
for k = 5:-1:2
    result = result * k;
end
A120
B20
CError: Invalid for loop range
D1
Attempts:
2 left
💡 Hint
Multiply numbers from 5 down to 2.
🔧 Debug
advanced
2:00remaining
Identify the error in this for loop
What error does this MATLAB code produce?
MATLAB
total = 0;
for i = 1 5
    total = total + i;
end
ASyntax error: Missing 'end' statement
BRuntime error: Variable 'total' undefined
CNo error, output is 15
DSyntax error: Missing colon in for loop range
Attempts:
2 left
💡 Hint
Check the for loop range syntax carefully.
Predict Output
advanced
2:00remaining
Nested for loops output
What is the value of 'count' after running this nested loop?
MATLAB
count = 0;
for i = 1:3
    for j = 1:2
        count = count + 1;
    end
end
A5
B6
C3
D2
Attempts:
2 left
💡 Hint
Multiply the number of iterations of both loops.
🧠 Conceptual
expert
2:00remaining
Effect of modifying loop variable inside for loop
Consider this MATLAB code. What is the final value of 'x' after the loop finishes?
MATLAB
x = 0;
for i = 1:4
    x = x + i;
    i = 10;
end
A11
B9
C10
D20
Attempts:
2 left
💡 Hint
Changing the loop variable inside the loop does not affect the iteration.