MATLAB Loops

Loops in programming

Concept of looping is used to execute instructions repeatedly. It helps coders to reduce lines of code by setting a condition. In looping first a condition is fixed, then set of instructions are repeated till the condition is satisfied. For example, if we want to send 1000 mails, we use a program where we sets looping statement to send those 1000 mails.

Every loop consists of three main parts:

1.initialization

2.condition

3.updation

MATLAB Loops

Only once the initialization takes place, then condition is checked the loop gets executed then updatation takes place and again condition is checked, this goes on till the condition fails to satisfy.

MATLAB LOOPS

IN MATLAB, we have for loop, while loop, nested loops to execute instruction on repeat.

MATLAB Loops

FOR LOOP

We use for loop when no. of repetitions to be executed are known. That means a fixed number of times a set of instructions are repeated until the condition is satisfied.

Syntax:

 for index = values
 <set of statements>
 …..
 ……
 …………
 end 

Flowchat:

MATLAB Loops

Example 1:

 for i= 1:2:8
             fprintf(‘i=%d \n’, i);
 end
 fprintf(‘after the loop i= %d  \n’, i); 
  • I will be assigned 1, then 3, then 5,………………….
  • So the expected output is
 i=1
 i=3
 i=5
 i=7
             After the loop i=7 it terminates. 

WHILE LOOP

We use while loop when we want to execute a group of statements for indefinite number of times.

Syntax:

 while <expression>
 <set of statements>
 …..
 …………
 ……………..
 end 

Flowchart:

MATLAB Loops

Example 1:

 p = 1;
s = 0;
% while loop execution example
 
while( p < 3 )
fprintf('the intermediate sum: %d\n', t);
    t = t + p;
    p = p + 1;
end 

Expected output:

 the intermediate sum is :0
 the intermediate sum is :1 

NESTED LOOP

Loops inside another loop is called nested loop. MATLAB also allows to use loop inside another loop.

Syntax for nested FOR loop

 for x=1:j
 for y=1:k
 <statements>;
             end
 end 

Syntax for nested WHILE loop:

 while <exp1>
 while <exp2>
 <statements>
             end
 end 

Flowchat:

MATLAB Loops

Example 1:

 Program to display a 2D matrix
 m=[1 2 3; 4 5 6; 7 8 9];
 [r c] = size(m);
 for row= 1:r
             for col =1:c
                         fprintf(‘element(%d,%d)=%d.\n’, row, col, m(Row, col))
             end
 end 

Expected output:

 element(1,1)=1.
 element(1,2)=2.
element(1,3)=3.
element(2,1)=4.
element(2,2)=5.
element(2,3)=6.
element(3,1)=7.
element(3,2)=8.
element(3,3)=9. 

MATLAB BREAK

This break statement creates a different loop which provides breaking point from the conventional loops. This terminates the execution of a loop.

Syntax:

break

Flowchat:

MATLAB Loops

Example 1:

 y = 10;
%here we are initialization a forever executing loop
while 1


 m = input('Enter number of loops: ');
            if m<= 0     
 %here we are terminating the loop execution     
 break
  end 
  for r = 1 : m
             y = y + 10
            end

end 

Expected output:

 Enter number of loops:
 4
 
y =
   50
y =
    60
y =
    70
y =
    80
 Enter number of loops:
 1
 y =
     90
Enter number of loops:
 2
 y =
    100
y =
   110

 

MATLAB CONTINUE

This statement passes control to next iteration of the current loop (it maybe FOR or WHILE loop).

Syntax:

continue

Flowchat:

MATLAB Loops

Example 1:

Using while loop

Let us take a set of integers values and you can discard few values like we discarded 10 from the WHILE loop and printed the remaining numbers from the range 6 to  15

 s = 5;
 %while loop execution
 while s<15
                s = s + 1;
                if s == 10
                                % skip the iteration
                                continue;
                end
 fprintf('we printing the values: %d\n', s);
 end
 printing the value of s: 6
 printing the value of s: 7
 printing the value of s: 8
 printing the value of s: 9
 printing the value of s: 11
 printing the value of s: 12
 printing the value of s: 13
 printing the value of s: 14
 printing the value of s: 15 

Example2:

Using FOR loop

Let us take a set of integers values and you can discard few values like we discarded 2 and 5 from theFOR loop and printed the remaining numbers from the range 1 to 10.

for p = 1 : 10

 if(p == 2 | p == 5)
            continue
            end
fprintf(‘value of p :%d\n’,p)
end 

Expected output:

 value of p : 1
 value of p : 3
 value of p : 4
 value of p : 6
 value of p : 7
 value of p : 8
 value of p : 9
 value of p : 10