Mail Adress

Have you a registration already ? Sign In

Please enter a valid email address.

Please confirm the membership and confidentiality agreement.

‘switch-case’ In MatLab Programming With Examples

- Home Page
- All Programs
- 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.

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

Download And Install MatLab 2019!

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.

‘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()’ command**, **we display this value in the command window.

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.

Do not forget to leave your comments and questions about the ‘while’ loop in Matlab below.

Your precious feedbacks are very important for us.

Similar Posts

- Site Comments

Popular Posts

Privacy & Cookies Policy

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.

Write Comments