Reputation: 61
I wrote this js code, and if I want to write is like a loop, which loop is best? while-loop,for loop, do-while loop.
var loan = 1000000;
var amoyear = 6000;
var answer = loan / amoyear;
document.write("A loan of 1000000 SEK is paid after "
+ Math.ceil(answer) + " years if installment is 500 kr / month. ");
It is very easy, but I can´t get my mind trough it.
Upvotes: 0
Views: 240
Reputation: 9505
Use for loop when you now the number of items before looping it. For example looping via elements of an array or array-like object:
var items = [];
....
for(var i = 0; i < items.length; i++){
...
}
If you iterate via fields of some object you may use the following form of for loop:
var obj = {};
...
for(var field in object){
//access each value as object.field
}
while-loop, is used, when you don't know the term when loop is finished.
do-while loop use also when you don't know the term when loop is finished but you need to iterate at least one time Compare:
do{
//do something, then check the term
}while(check the term)
vs.
while(check the term){
//do something
}
PS: there is a form of for loop - so called foreach, but i'm not sure it is standard, so I don't use it yet.
Upvotes: 0
Reputation: 29160
Just taking a superficial look at your unformatted code.. it looks like you want to do a loop to calculcate the amount of time different loans will take to pay off. When your working with numbers, it's usually easier to work with a for loop. The following examples both calculate time to pay off loans between $100,000 and $20,000,000 (at intervals of $100,000)
var maxLoanAmount=20000000;
var amoyear = 6000;
for (var loan = 100000; loan<=maxLoanAmount; loan = loan + 100000){
var answer = loan / amoyear;
document.write("A loan of " + loan + " SEK is paid after " + Math.ceil(answer) + " years if installment is " + (amoyear/12) + " kr / month. ");
}
although you can easily do the exact same thing with a while loop.
var maxLoanAmount=20000000;
var amoyear = 6000;
var loan = 100000;
while (loan<=maxLoanAmount){
var answer = loan / amoyear;
document.write("A loan of " + loan + " SEK is paid after " + Math.ceil(answer) + " years if installment is " + (amoyear/12) + " kr / month. ");
loan = loan + 100000;
}
Upvotes: 0
Reputation: 54087
(Approaching your question as a general question about loop constructs and how they are 'typically' used)
It depends on what your exit condition is...
for
loop if you want to iterate a fixed number of times and need to use an incrementing (or decrementing) value in your loopwhile
loop if your exit condition can be expressed as a boolean statement, and the minumum number of iterations of your loop is zero (because the boolean statement is located before the loop code)do...while
loop if your exit condition can be expressed as a boolean statement, and the minumum number of iterations of your loop is one (because the boolean statement is located after the loop code)Upvotes: 3
Reputation: 24360
A rough, generic answer to your question (not considering your particular use-case):
If you want to loop over an interval, increasing/decreasing a value by a fixed amount in each step, then use a for-loop since it is very idiomatic for that (in almost any imperative or object-oriented language).
For almost all other purposes, use the while-loop.
I'd say that do-while is not very useful and not seen often, even if you of course can find situations for that too. But some slight refactoring can often change it into a while-loop that is (usually) more readable anyway.
Upvotes: 0
Reputation: 22064
I think you want to perform the division part with loop though what you have done is more efficient.
I hope this is not a homework :)
Here is the answer:
var loan = 1000000;
var amoyear = 6000;
var answer;
for(answer=1; amoyear*answer<=loan; answer++);
The above code uses simple for loop and I think it would be the best for this situation.
Upvotes: 0