## Introduction to While loop in MATLAB

MATLAB is a scientific programming language which 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.

MATLAB is so popular that it is used 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, MATLAB 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.

### Syntax

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

`while expression`

statements

end

#### Explanation: –

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

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

**Example: –**

`a = 10;`

% while loop execution example

while( a < 20 )

fprintf('value of a: %d\n', a);

a = a + 1;

end

#### Explanation of the Example: –

- We define a variable to be equal to 10
- A line starting with % is the comment in MATLAB, so we can ignore the same.
- 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.
- Next line prints the current value of a and after that, the next line is executed
- 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.
- 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.

4.5 (1,791 ratings)

View Course

### Flow diagram

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

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

- There has to be some Boolean condition which would be evaluated by the loop.
- 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.
- 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:

`N=1;`

While N < 5

N = N-1

end

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 (Proper coding with output)

Let’s talk 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;

end

Output: –

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;

end

Output: –

intermediate sum = 0 intermediate sum = 1

**Example 3: –**

`i = 1;`

while (i < 3)

i

i = i + 1;

end

Output : –

i = 1 i = 2

### Conclusion

With the above examples and explanations, I hope the article gave a fair idea of while loop in MATLAB. Students are advised to run the examples shown in the MATLAB command window and see for themselves how the output is coming. As a next step, students are advised to make small changes in the example codes and run the same to see what the effect on the output is. Thereafter, they can also practice various examples available on the web or try to create some simple exercises yourself. Coding is all about practice, so keep practicing as much as possible.

### Recommended Article

This has been a guide to While Loop in Matlab. Here we discuss the concepts and some examples of While Loop in Matlab to understand it better manner. You can also go through our other suggested articles to learn more –