Out of my surprise, the for loop for javascript does not seems to allow arithmetic expression inside the for ( ; ; ) {}.
Is this also true for other languages?
for loop with arithmatic expression
Moderator: General Moderators
Re: for loop with arithmatic expression
Perhaps... You have an example?
Re: for loop with arithmatic expression
I spent several days to hunt for the reason why my loop do not terminate:requinix wrote:Perhaps... You have an example?
function start(str){
isb = parseInt(str.substr(0,12));
for (isb; isb < (isb+3); isb++){
isbn=fix_CheckSum(isb+'');
send_findbooks(isbn);
};
}
When I found the mistake...and went back to look up books and conduct internet search....indeed never an example had an arithmetic expression inside........
I thought an advanced programing language like javascript every statement can be compound.
Is this a general rule?
Re: for loop with arithmatic expression
The problem is just your logic: you're comparing the loop variable against itself+3. It will always be less than itself+3.
Re: for loop with arithmatic expression
requinix wrote:The problem is just your logic: you're comparing the loop variable against itself+3. It will always be less than itself+3.
oh...really? So, isb+3 will be re-evaluated ever time? (I though it will only be evaluated once at the very beginning.)
so, if I use other arithematic expression will be o.k.?
Re: for loop with arithmatic expression
The whole thing is evaluated: less-than and addition and all. If you made a copy of isb+3 before the loop and compared isb to that, it would work. Or you can simply loop i=0 to 3 and use isb+i inside.wvoyance wrote:oh...really? So, isb+3 will be re-evaluated ever time? (I though it will only be evaluated once at the very beginning.)
Re: for loop with arithmatic expression
yes, thanks. I cannot image I made such a simple mistake.