The other main type of loop that is used in MATLAB is a While loop. While loops are used when you might be uncertain of the number of times you need a loop repeated. Let's learn this one by doing one. Some code to put in a script. Note, if you copy and paste this it may not work. You will have to type it in.
clc;
clear all;
close all;
balance = 10000;
goal = 1000000;
current_year = 2017;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
while balance < goal
balance = balance * 1.05;
current_year = current_year + 1;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
end
So what does this code do? It is supposed to simulate a savings account which has an annual interest rate of 5%. So, each year balance grows by 1.05. The loop stops when the balance exceeds $1,000,000. If you execute this code you should find it takes until 2112 to save more than a million dollars.
While loops can be very useful when programming experiments or other types of code - for instance a loop that is not over until the user hits a key. However, a warning! If you construct your while loop poorly you will get stuck in an infinite loop. Look at the code below BUT DO NOT RUN IT!
clc;
clear all;
close all;
balance = 10000;
goal = 1000000;
current_year = 2017;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
while balance < goal
balance = balance / 1.05;
current_year = current_year + 1;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
end
Do you see the problem? An opposed to balance increasing yearly, it is now decreasing. As such, balance will never be greater than goal and this loop will never end. If this happens you typically have to crash MATLAB and restart it.
Challenge
Using what you have learned so far, write a script that has used a while loop to add 2 to a variable x 100 times. If you get stuck sample code is HERE. Hint. While loops do not have a built in counter like for loops do. You will have to create a variable loop_counter BEFORE you begin your while loop and add to it within the loop by stating:
loop_counter = loop_counter + 1;
You can move onto the next tutorial now.
clc;
clear all;
close all;
balance = 10000;
goal = 1000000;
current_year = 2017;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
while balance < goal
balance = balance * 1.05;
current_year = current_year + 1;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
end
So what does this code do? It is supposed to simulate a savings account which has an annual interest rate of 5%. So, each year balance grows by 1.05. The loop stops when the balance exceeds $1,000,000. If you execute this code you should find it takes until 2112 to save more than a million dollars.
While loops can be very useful when programming experiments or other types of code - for instance a loop that is not over until the user hits a key. However, a warning! If you construct your while loop poorly you will get stuck in an infinite loop. Look at the code below BUT DO NOT RUN IT!
clc;
clear all;
close all;
balance = 10000;
goal = 1000000;
current_year = 2017;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
while balance < goal
balance = balance / 1.05;
current_year = current_year + 1;
disp(['In ' num2str(current_year) ' we have a balance of $ ' num2str(balance)]);
end
Do you see the problem? An opposed to balance increasing yearly, it is now decreasing. As such, balance will never be greater than goal and this loop will never end. If this happens you typically have to crash MATLAB and restart it.
Challenge
Using what you have learned so far, write a script that has used a while loop to add 2 to a variable x 100 times. If you get stuck sample code is HERE. Hint. While loops do not have a built in counter like for loops do. You will have to create a variable loop_counter BEFORE you begin your while loop and add to it within the loop by stating:
loop_counter = loop_counter + 1;
You can move onto the next tutorial now.