2. Control Statements

While loop in MATLAB

MATLAB is a scientific programming language that is used a lot for research and academic purposes. A lot of industries are also using it, but universities and research organizations are the main customers of this proprietary software tool. MATLAB is developed by MathWorks and in order to use it, one must purchase a license. For students and beginners, however, it does come with a limited-time trial version.

It is so popular that it is used by colleges and universities across the world to teach scientific computation and engineering such as signal processing, control system, advanced mathematics, and many other subjects. Today, even after the easy availability of open-source software such as R and Python for similar work, it is still popular and used extensively.

In this article, we provide basic building blocks of MATLAB and specifically focus on a while loop. If you are familiar with other programming languages then you must be aware of loops such as for loop, if loop, while loop, etc. We shall talk about the same in the context of MATLAB. And, if you are completely new to programming then as well you need not worry as the discussions in this article is at absolute beginner level and you are not required to have a programming background. But, it is expected that you know the basic data types in MATLAB.


In this section, we shall provide syntax of a while loop and then subsequently explain the same in step by step method.

while expression


  1. While is the keyword for while loop.
  2. An expression is a condition that needs to be true for the while loop to work.
  3. Statements are the actions that would be executed if the condition or expression is true.
  4. The end is the keyword which suggested the closure of the loop.

To better understand the syntax, let’s take an example.


a = 10;
% while loop execution example
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;

Explanation of the Example

  1. We define a variable to be equal to 10
  2. A line starting with % is the comment in MATLAB, so we can ignore the same.
  3. While loop starts and the condition is less than 20. What it means is that the while loop will run till the value of a is less than 20. Note that currently, the value of a is 10.
  4. Next line prints the current value of a and after that, the next line is executed
  5. The line a=a+1, adds 1 to a and then the while condition is again checked. Now the value of a is 11 which is still less than 20 so the loop runs again.
  6. Finally, the loop runs until a attains the value 19 and after that, it stops.

The detailed output will be shown in the example section.

Flow diagram

Let’s understand the flow diagram of a while loop for better clarity.

Boolean box

The flow diagram above is quite simple and self-explanatory. It is the same as the steps described above in the syntax section. When the Boolean condition is true the statements are executed otherwise it does nothing and loop execution stops.

How while Loop Works in MATLAB?

The working of the while loop is quite clear from the flow diagram above. A step by step explanation of syntax is also provided in the syntax section. In this section, we shall explain the same ideas in more detail.

A while loop has mainly three parts that need to be understood.

  1. There has to be some Boolean condition that would be evaluated by the loop.
  2. There needs to have some action for that Boolean condition. i.e. what happens when the condition is true and what happens when the condition is false.
  3. The control of the loop moves according to the nature of the condition i.e either it computes something, or it stops working.

Based on the condition provided, a while loop can run for a finite number of times producing finite output or it can go for as long as possible unless stopped manually.

An infinite loop may look like below:

While N < 5
N = N-1

You may note here that as the value of N is decreasing in each iteration, it will always satisfy the condition and hence will continue working infinitely. Is not it simple and yet so powerful?

Examples of while loop in Matlab

Let’s talk about a few examples to understand further.

Example 1 – This is the same example as above

a = 10;
% while loop execution example
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;


Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19

Example 2:

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


intermediate sum = 0
intermediate sum = 1

Example 3:

i = 1;
while (i < 3)
i = i + 1;


i = 1
i = 2

Leave a Reply

Your email address will not be published. Required fields are marked *