Academic Block

Logo of Academicblock.net

Loops in MATLAB

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. Here’s an example:

% Using a for loop to compute squares of numbers from 1 to 5
% This loop will run 5 times (i=1:5).
% In each loop run value of i will be incremented by 1.
% Example: for 1st loop i=1, for 2nd loop i=2, and so on.

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 is true. Here’s an example:

% Using a while loop to compute a factorial
n = 5;
factorial = 1;
% this loop will run till the value of n becomes less or equal to 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 outer 'for' loop (i = 1:rows)
% complete inner loop (j = 1:cols) will run
% Example: for i=1; inner for loop will run from j=1 to j=cols
% then for i=2, inner will run again from j=1 to j = cols
% this process will continue till last iteration of outer loop (i=rows)

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 in Matlab

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; % It skips other remaining statements in the current loop
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; % when i=5, break terminates the loop's execution
end
disp(i);
end

Output:

1
2
3
4
    

Using end

The end statement is used to close loops, if statements, and other blocks of code:

% Using end in a loop and 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 for Loops

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.