# Explanation Of ‘while’ Loop In Matlab With Examples

Like in other programming languages, you can also code loops in Matlab®. Along with the for-end loop, while loop is also a loop programming command in Matlab®. The use of the while command in Matlab® to code loops is very easy. Just take a look at the examples below.

MATLAB For Beginners: A Gentle Approach

If you are interested to learn Matlab® at an engineering level, click on the given link or the ‘Shop Now’ button to check the recommended book by Mechanicalland, from Amazon!

On this post

## How To Use ‘while’ Loop In Matlab®?

``````>> x = 5;
y = 1;
while x>y
y=y+1;
end
y

y =

5

>> ``````

Here you can see a very basic example of the use of the ‘while’ loop in Matlab®. There are starting values of ‘x’ and ‘y’ variables, which are 5 and 1 respectively. In the ‘while’ loop, we want to increase the value of ‘y’ up to the value of ‘x’. So, we need to stop the working of code when the values of ‘x’ and ‘y’ are equal.

To do this, we used the ‘while’ code that the code inside the loop will work while the condition x>y is true. In each loop, we increase the value of ‘x’ for 1.

Then you can see the last value of ‘y’ at the command window in the example above in Matlab®.

Take a look at the example below about ‘while’ command in Matlab®.

``````>> a = [ 2 5 6 3 66 33 22];
y =1;
while a(y)<60
y=y+1;
end
disp('The first number of vector a bigger than 60;')
disp(a(y))
The first number of vector a bigger than 60;
66

>> ``````

In this example about the use of the ‘while’ command, we want to find the first element of vector ‘a’ which is bigger than 60. To do it, we assigned a counter ‘y’ which has an initial value of 1.

In the ‘while’ command, we question all the elements of vector ‘a’ by typing the counter inside ‘a’. If the element of ‘a’ is smaller than 60, code inside while will increase the value of counter 1.

If the element of ‘a’ is bigger than 60, the Matlab® compiler will exit the while loop, then disp() commands will work to show the first element of a which is bigger than 60.

YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn Matlab®!

As result, you can see this as 66 in the command window.

## Use Of ‘break’ Command In While Loop

‘Break’ is a very useful command to halt the all program if the statement is true in Matlab®. ‘Break’ is generally used in if-else queries in Matlab® programming. Take a look at the example below; 1

``````>> a=[2 6 3 45 6 2 3 6 5 6 3];
i = 1;
while a(i)>0
i = i+1;
if a(i) == 45
break
end
end
disp(['the ' num2str(i) 'th element is 45.']);
the 4th element is 45.
>> ``````

At the example above which is executed in the Matlab® command window, we created a vector ‘a’ that has various integers inside it. Also, we created a variable ‘i’ which has the value of 1, to build the ‘while’ loop.

In the ‘while’ loop, we want to find the address of the element which has the value of 45. So, inside the ‘if-else’ query inside the ‘while’ loop, we stop the loop if the value of the element is 45. So, the value of ‘i’ after the execution of the ‘break’ command, will give the address of element of ‘a’ which has the value of 45.

By using the ‘disp()’ commandwe display this value in the command window.

## Conclusion

As you see in the examples above, while loop is a very useful loop command that can be used in Matlab® programming.

If you wish for further examples about the use of the ‘while’ loop, leave your requests in the comment section.