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 

Related Topics

Matlab Calculus

Analytics Calculus is significant in various fields like designing, physical science, and different sciences, yet the estimations are finished by machines or PCs, regardless of whether they are into Physics...

3 minutes read.

MATLAB Variables

What are variables? A variable is a storage location or a container to store or hold data. In programming, variables are used to reservedata created by the programmers so that it...

4 minutes read.

Matlab Square Wave Signal Generation

What is Square Wave signal Generator? A “square wave” is a sort of non-sinusoidal waveform, most normally experienced in gadgets and sign preparing. An optimal square wave substitutes consistently and momentarily...

3 minutes read.

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,...

3 minutes read.

MATLAB Control Statements

What are control statements? The name itself suggests that something is being controlled, these control statements decide or alter the flow of execution ina program. In program, we often need to...

3 minutes read.

MATLAB vs Other Languages

MATLAB vs Other Languages If we try to find which is the best computer programming languages, the answer is not straightforward, and that might sound similar to asking which is the...

3 minutes read.

MATLAB Linear Convolution problem solving

What is linear convolution? In science (specifically, useful examination), convolution is a numerical procedure on two capacities (f1 and g1) that communicates how the state of one is adjusted by the...

4 minutes read.

MATLAB’s Array

What is an Array? An array is a collection of similar datatype elements in contiguous memory location. Every element in array is stored with an index number starting from 0. First...

4 minutes read.

MATLAB Butterworth filter

What is Butterworth filter? The Butterworth channel is a kind of sign preparing channel intended to have a recurrence reaction as level as conceivable in the passband. It is likewise alluded...

3 minutes read.

Matlab function

Introduction: Functions in MATLAB are composed with code that link one variable with another, and yielded output is connected precisely to one specific information that shapes a significant piece of any...

6 minutes read.

MATLAB Scripts

Scripts Introduction Matlab Script represents some programs and is executed like we execute command in the command window of Matlab’s environment. Matlab script is also defined as a sequence of commands;...

6 minutes read.

matlab Integral

What is integration? In maths, Integral allots numbers to functions in a manner that portrays relocation, region, volume, and different ideas that emerge by consolidating tiny information. The way toward discovering...

5 minutes read.

Matlab Algebra

Up until now, we have seen that every one of the models work in MATLAB just as its GNU, then again called Octave. In any case, for addressing fundamental mathematical...

6 minutes read.

Matlab Matrix

Matlab Matrix What is a Matrix? a rectangular array of expressions arranged in particular rows and columns that is treated as a solitary substance and controlled by specific standards. MatlabMatrix A Framework called matrix,...

5 minutes read.

matlab polynomials

In mathematics, a polynomial is an articulation comprising of indeterminates factors and the respective coefficients, that includes just the tasks of augmentation, expansion, deduction, and non-negative whole number exponentiation of...

4 minutes read.

Matlab Unit impulse signal generation

What is the Necessity of learning Matlab programs? Matlab Programming Examples give you an idea of typical Matlab programs in a nutshell.Matlab programming should be utilizing object-oriented programming, GUI programming, and...

3 minutes read.

Differentiation in Matlab

Differentiation in Mathematics In maths, Derivatives are an essential device of math or calculus. For instance, derivative of the situation of a moving article regarding time is the item's speed: this...

6 minutes read.

MATLAB Features

MATLAB Features MATLAB (abbreviation for matrix laboratory) is an interactive programming environment that eases high-power computations in the IT world. Initially, it was developed with the intention of matrix calculation only....

4 minutes read.

MATLAB Simulink examples

We will discuss few examples to understand different use-cases of MATLAB’s Simulink. Simulink is basically MATLAB-based. Its essential interface is a graphical square charting apparatus and an adaptable arrangement of square...

4 minutes read.

Matlab DFT and IDFT

Matlab Program- (DFT and IDFT of a sequence) Program for DFT (Discrete Fourier change) and IDFT (Inverse discrete Fourier change) of a sequence. Program explanation: In Arithmetic, the discrete Fourier change (DFT) changes...

3 minutes read.