Complete the code to create a for loop that counts from 1 to 5.
for i = [1] disp(i) end
The syntax 1:5 creates a range from 1 to 5 in MATLAB, which is used in the for loop.
Complete the code to sum numbers from 1 to 10 using a for loop.
total = 0; for n = 1:10 total = total + [1]; end
Inside the loop, n is the current number from 1 to 10, so we add it to total.
Fix the error in the for loop to display numbers from 5 down to 1.
for i = [1] disp(i) end
5:1 which counts up and results in an empty loop.1:-1:5 which is an invalid range.To count down from 5 to 1, use the syntax 5:-1:1 which means start at 5, step by -1, end at 1.
Fill both blanks to create a for loop that prints only even numbers from 2 to 10.
for i = [1]:[2]:10 disp(i) end
The loop starts at 2 and steps by 2 to print even numbers up to 10.
Fill all three blanks to create a for loop that prints the squares of numbers from 1 to 5.
for [1] = [2]:[3]:5 disp([1]^2) end
The loop variable i goes from 1 to 5 in steps of 1, and we display its square.