MATLAB: Does the code stop doing iterations

breakfor loop

When the car loan has been paid off, the student loan value goes to 0 as well.
clc; clear; % clear the command window and memory
x = 2000; % starting balance of the current account
r = 0.015/12; % the interest rate on the current account
sal = 28650; % graduate's salary
RPI = 0.017; % RPI value
liv = 1506; % monthly living expenses
stloan = 27000; % starting sum of the student loan
if sal<21000
rep = 0; % monthly student loan repayment


rs = RPI/12; % monthly student loan interest

elseif sal<41000
rep = 0.09*(sal-21000)/12; % monthly student loan repayment
rs = (RPI+0.03*(sal-21000)/20000)/12; % student loan interest
else
rep = 0.09*(sal-21000)/12; % monthly student loan repayment
rs = (RPI+0.03)/12; % monthly student loan interest
end
carloan = 15700; % starting sum of the car loan
carrep = 357; % monthly car loan repayments
carrs = 0.05/12; % monthly car loan interest
N = 120; % the number of months to consider
t = 1:N+1; % our timeline
xhist=zeros(1,N+1); % create a vector to hold the account history
xhist(1) = x;
sthist=zeros(1,N+1); % vector to hold the student loan history
sthist(1) = stloan;
carhist=zeros(1,N+1); % vector to hold the car loan history
carhist(1) = carloan;
for m=1:N % commands inside the loop are executed for each month
x = (1+r)*x+sal/12; % apply the interest and pay in the salary
stloan = (1+rs)*stloan; % apply the interest to the student loan
carloan = (1+carrs)*carloan; % apply the interest to the car loan
x = x-liv;
if stloan-rep>0
x = x-rep; % take money off the current account...

stloan = stloan-rep; % ...and make student loan payment
else
x = x-stloan; % take the remainder off the current account...

stloan = 0; % ...and close the student loan
end
if carloan-carrep>0
x = x-carrep; % take money off the current account...
carloan = carloan-carrep; % ...and make car loan payment
else
fprintf('Month of Final Car Loan Payment = %.2f\n',m); % print the month of final payment
fprintf('Final Car Loan Payment = %.2f\n',carloan); % final loan payment
x = x-carloan; % take the remainder off the current account...
carloan = 0; % ...and close the car loan
break
end
xhist(m+1) = x; % save the sum at the beginning of month m
sthist(m+1) = stloan; % do the same for the student loan
carhist(m+1) = carloan; % do the same for the car loan
end
fprintf('Balance = %.2f\n',x); % balance at the beginning of (N+1)th month
fprintf('Student Loan Balance = %.2f\n',stloan); % print the outstanding student loan balance
fprintf('Car Loan Balance = %.2f\n',carloan); % print the outstanding car loan balance

Best Answer

Because when m = 49, you hit the break command, and leave the for loop. See the documentation for break for details.
Related Question