In today’s lesson, we’re going to cover the following topics:
1. While Loops
2. Infinite Loops
3. Nested While Loops
4. Break Statements
5. Return Statements
VIDEO COMING SOON
While Loops
This is one of the 4 types of control flow logic (recall the 4 types – if-else, switchcase, while loops and for loops). A while loop is similar to a for loop, except that it does not specify how many times the loop will run, like a for loop does. With while loops, the loop will keep running while a condition is satisfied, rather than running for specific values.
For example, if you have a vector [1 2 3 4 5] and you want to multiply each value in the vector by 2. You would use a for loop to keep the loop executing so that every number is multiplied by 2, until all the numbers in the vector have gone through that. Usually you do not use Relational Operators such as greater than, less than (>, <, >=, <=), or Logical Operators such as the or operator (|), other comparison operators.
With a while loop, we wouldn’t specify this type of condition for a loop to run through all values in a vector. An example of when we could use a while loop is if we wanted a user to enter a number, and if it was positive then it should be multiplied by 2. In this case, the condition is that the number a user enters has to positive, and if it is then the loop will multiply that value by 2. This will run for all values that are positive, every time there is a new number input. Notice how we didn’t have a set limit of numbers during which the loop will execute like with a for loop.
Here is the syntax for a while loop:
There are 3 parts to a while loop. The words in blue are mandatory and do not change, and the words in black are what you will change according to your code:
(Line 1) while expression – Replace the word expression with the condition you want to satisfy in order for the code within the while loop to execute.
(Line 2) statements – Replace the word statements with the code you would like to execute if the condition in the line above is satisfied.
(Line 3) end – The end statement is always there to end the loop. If you don’t use this, everything after the while statement will keep executing.
Let’s do an example. Say we want the user to enter a number. If the number is positive, MATLAB will multiply that number by itself.
How do we think through this problem?
(1) The question asks that the user enters a number – this means we need to use an input statement. We will set this equal to the variable x.
(2) Now we want to check whether the number entered is positive, and we can do this with a while loop (some of the other control flows will work as well, but a while loop works well for this). We will set the condition that x > 0 (x has to be greater than zero, meaning it has to be a positive number).
(3) Then we will get that number to multiply by itself in the statements section if the number is positive – multiplying by itself means squaring the value, so we will have x^2.
Putting this together in a code gives us this:
Now running the code prompts the user to enter a number:
I entered 5, and when I hit Enter, the loop kept repeating over and over to give me 25, which is 5^2. I had to pause the code because the loop wouldn’t stop executing.
Infinite Loops
When the situation above occurs, it is called an infinite loop. See how the answer 25 came up again and again in the output above, because there is no condition to stop the loop – as long as the number entered was positive, it will keep looping back on itself.
How do we fix this?
(a) One way is to use a different control flow – try an if-else construct instead of a while construct. Click here if you missed the lesson on if-else.
(b) If you really want to use a while statement, you can put a break within the loop. This will only make it loop once:
Running the code and entering 5 as an input only shows the output of 25 one time:
Nested While Loops
We can have a while loop within another while loop if the code needs to have two statements that must be satisfied, so these two loops run simultaneously. This is what the syntax looks like:
Break Statements
Break is a statement that can be used to exit the current for or while loop. If you are in the middle of a loop and want it to stop and go back to the beginning, you can use the word break. Let’s take a look at an example.
Say I have the following while loop. If the user enters a number, and that number is 1, then within the while loop is says break, followed by the calculation a+1. Since break is the first thing in the loop, then if the user’s input IS equal to 1, the condition is met – but the calculation a+1 will never be done, because the break statement will exit out of the loop.
Let’s try running this code and entering 1 as an input:
See how the output is still 1, rather than 2 (if the code executed the line a+1, then 1+1 would’ve been 2):
Now let’s try moving the break statement to AFTER the a+1:
Now running the code and entering an input of 1 will show me and value of a = 1, as well as b = 2 since b = a+1 (so 1+1):
Return Statements
Return is a statement that can be used to return the value of a variable at a specific point, or to just display a value at a specific point (this does not mean to “return” to the beginning of the code). You can use return anywhere, but it exits the loop it’s in, as well as the entire script/function.
If we had the same code as we do for the break example above, replacing break with return will do the same thing, since it’s a pretty short code. If, however, there were other loops within the code, then using break would just exit the one specific loop, whereas return would exit the entire script/function, and everything within it.