# While-loop (with an example)

#### sarahzz

##### New Member
Hello!

I am trying to understand the concepts of while-loops for programming, and was watching this tutorial on youtube. The example was given in Matlab (which I believe has a similar syntax to R):

bal = 5000;
year = 0;
while bal<1000000
bal = 1.08*bal + 5000;
year = year + 1;
end

disp(bal);
disp(year);

Just to briefly explain the problem, although it might be obvious to many of you:
I have a starting balance of 5000 in the bank invested at a compound interest rate of 8%. At what year will my balance amount to 1 million?

the disp(bal) and disp(year) give me the correct answers, but I also want to plot the graph of balance(Y-axis) against year(X-axis), to show the exponentially-increasing savings amount. However when I code:

plot(year,bal)

All I get is a blank space. My friend suggested that it's possible that I'm only plotting a dot. Can anyone help me solve the problem of plotting this graph?

Thank you!

#### Dason

##### Ambassador to the humans
I don't use matlab but you would need to store all of your values. Currently you're only storing the last value of year and bal. So you would need to use an array or list type structure to keep track of the accumulated data on bal and year.