# While loop is continuous.

3 visualizaciones (últimos 30 días)
jack bennet el 28 de Ag. de 2012
Im trying to write a homeloan function that calcs certain values(deposit, interest etc) and displays an error message if the deposit is less that %20 of the house price.
Only problem is, the function loops continuosly. Any idea how to fix this?? My SCRIPT is below. cheers
function [monthlyPayment, interest] = homeloan(price, deposit, intRate, lengthOfLoan)
% for deposit while using a while loop
deposit <= ((300000)/ 5);
while deposit < 300000
disp('error');
deposit= ((300000)/ 5); % do not forget to do this, otherwise we would loop forever
%monthly payment
monthlyPayment = (((price - deposit)*intRate)*((1 + intRate)^lengthOfLoan))/(((1 + intRate)^lengthOfLoan) - 1)
% interest
interest = (monthlyPayment * lengthOfLoan) - ( price - deposit)
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Azzi Abdelmalek el 28 de Ag. de 2012
Editada: Azzi Abdelmalek el 28 de Ag. de 2012
deposit= ((300000)/ 5); % if you do this, you will loop forever
can you post the full code

Iniciar sesión para comentar.

### Respuestas (3)

Walter Roberson el 28 de Ag. de 2012
deposit <= ((300000)/ 5);
is a logical test, followed by throwing away the result. It is not an assignment.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Arthur el 28 de Ag. de 2012
You're not changing the value of deposit in the loop, but always setting it at 300000/5. So deposit is always smaller than 300000, and the loop will run forever.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Image Analyst el 28 de Ag. de 2012
Try this. Just check to see if the deposit is big enough and warn user if it's not:
function [monthlyPayment, interest] = homeloan(price, deposit, intRate, lengthOfLoan)
monthlyPayment = [];
interest = [];
minDeposit = price * 0.2;
if deposit < minDeposit
warningMessage = sprintf('Error: your deposit of %.2f is less than the required deposit of %.2f, which is 20%%of %.2f',...
deposit, minDeposit, price);
uiwait(warndlg(warningMessage));
else
% Calculate monthly payment
monthlyPayment = (((price - deposit)*intRate)*((1 + intRate)^lengthOfLoan))/(((1 + intRate)^lengthOfLoan) - 1)
% Calculate interest
interest = (monthlyPayment * lengthOfLoan) - ( price - deposit)
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Whos en Help Center y File Exchange.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by