Understanding For Loops -


i'm trying out loop. added if statement stop loop once reaches 30. have seen i <= 10 run 11 times, since loop still run when reaches 10.

why code below run 11 times (first print line) if there if statement sets i 0 when reaches 10? shouldn't print 10 asterisks instead of 11 - since never reaches 11th loop? also, second if sets i 10, should let loop run 1 more time, through first if, , set i 0?

int j = 0; (int = 0; <= 10; i++) {       console.write("*");     if (i == 10)     {         j++;         console.writeline("");         = 0;      }      if (j == 30)      {          = 10;      }  } 

on first loop, line has 11 stars, because i iterates 0 through 10, total of 11 iterations.

whenever i becomes value 10, j incremented, newline printed, , i becomes 0.

however, when i set 0 within loop, loop makes i iterate 1 10, total of 10 iterations.

this because i incremented before next iteration starts.

a for loop structure:

for (init; condition; increment) {     body } 

is more or less equivalent while loop:

init while (condition) {     body     increment } 

the caveat when body has continue statement, jumps down increment part.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -