While Loops

 
v = 2;
i = 2;
while v(i-1)<1000
    v(i) = 2^i;
    i=i+1;
end

disp(v)
>>  2   4   8   16   32   64   128   256   512   1024
v = 0;
i = 1;
while true
    v(i) = 2^i;
    if v(i)>1000
        break;
    end
    i=i+1;
end

disp(v)
>>  2   4   8   16   32   64   128   256   512   1024

Compound interest:

You invest $1000 at 8% p.a. interest rate. When will the amount double?

amount(1) = 1000
r = 0.08

n = 1;
while amount(n)<2000
    amount(n+1) = amount(n)*(r+1);
    n = n+1;
end

n
>> 11   % The amount will double in the 11th year