简体   繁体   中英

Stuck in an Infinite Loop with Multiple If Conditions inside a loop

Can you please let me know why this code is generating an infinite loop? I have tried to limit the steps between > min ) && (i <= logic but it's still sticking in the loop.

 var min = -9.00; var max = 14.00; for (var i = min; i < max;) { console.log(i); if((i > min ) && (i <= -2.00)) {i += (0.25);} if((i > -2.00 ) && (i <= 0.00)){i += (0.5);} if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);} if((i > 6.00 ) && (i <= max)) {i += (0.5);} }

Yes you have an infinite loop. Because i = -9.00 and your first if is i > min which min = -9.00 its the same not greather. just change > to >=

 var min = -9.00; var max = 14.00; for (var i = min; i < max;) { console.log(i); if((i >= min ) && (i <= -2.00)) {i += (0.25);} if((i > -2.00 ) && (i <= 0.00)){i += (0.5);} if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);} if((i > 6.00 ) && (i <= max)) {i += (0.5);} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM