Academic Block

Loops in MATLAB Get Certified

Loops are essential constructs in MATLAB programming, allowing repetitive tasks to be automated efficiently. MATLAB provides two primary types of loops: for and while. Nested loops can also be used for more complex operations.

Using for Loop in MATLAB

A for loop is used to iterate over a sequence of values:

% Using a for loop to compute squares of numbers from 1 to 5
% This loop will run 5 times (i = 1:5).
% In each iteration the value of i is incremented by 1.

for i = 1:5
    disp(i^2);
end

Output:

1
4
9
16
25

Using while Loop in MATLAB

The while loop runs as long as a condition remains true:

% Using a while loop to compute a factorial
n = 5;
factorial = 1;
% The loop continues while n is greater than 0

while n > 0
    factorial = factorial * n;
    n = n - 1;
end
disp(factorial);

Output:

120

Nested Loops in MATLAB

Loops can be nested to perform more complex tasks, such as working with matrices:

% Using nested loops to create a multiplication table
% For each iteration of the outer loop the complete
% inner loop is executed.

rows = 5;
cols = 5;
for i = 1:rows
    for j = 1:cols
        fprintf('%d ', i * j);
    end
    fprintf('\n');
end

Output:

1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 
4 8 12 16 20 
5 10 15 20 25 

Using continue, break, end, and return

Using continue

The continue statement skips the remaining code in the current iteration and moves to the next iteration:

% Skipping multiples of 3 in a loop
for i = 1:10
    if mod(i, 3) == 0
        continue;   % skip the rest of this iteration
    end
    disp(i);
end

Output:

1
2
4
5
7
8
10

Using break

The break statement exits a loop prematurely:

% Exiting a loop when a condition is met
for i = 1:10
    if i == 5
        break;      % terminate the loop when i equals 5
    end
    disp(i);
end

Output:

1
2
3
4

Using end

The end statement closes loops, if statements and other blocks of code:

% Using end in a loop and a condition
for i = 1:5
    if i == 3
        disp('Middle point reached');
    end
    disp(i);
end

Output:

1
2
Middle point reached
3
4
5

Using return

The return statement exits a function or script immediately:

% Stopping execution of a script
for i = 1:10
    if i == 5
        disp('Stopping execution');
        return;
    end
    disp(i);
end

Output:

1
2
3
4
Stopping execution

Useful MATLAB Functions / Keywords for Loops

Keyword / Function
Explanation
for
Executes a block of code a specified number of times.
while
Executes a block of code while a condition is true.
continue
Skips the rest of the code in the current iteration and moves to the next iteration.
break
Exits a loop prematurely.
end
Marks the end of a block of code for loops, if statements and functions.
return
Exits a function or script immediately.

Practice Questions

Test Yourself

1. Use a for loop to calculate the sum of all integers from 1 to 100.

2. Write a while loop to compute the Fibonacci sequence up to 50.

3. Create a multiplication table for numbers 1 through 10 using nested loops.

4. Modify the nested-loop example to skip printing multiples of 3 using the continue statement.

5. Write a script that uses break to exit a loop if a randomly generated number is divisible by 7.