+ 2

Just need a bit of help understanding the output of this loop.

function loop(){ var a = 0; var b = 8; do{ a++ } while (a<b); return a; } console.log(loop()); So the output is 8, however I thought with < not being <= I had thought the loop would finish at 7 not 8.

10th Jun 2020, 11:52 AM
Michael Smyth
1 Odpowiedź
+ 3
Michael Smyth for value of "a" equal to 7, loop's condition will still be fulfilled and loop will run once again. Once the value of "a" is 8, then condition becomes false and loop breaks.
10th Jun 2020, 11:59 AM
Arsenic
Arsenic - avatar