Which of the following loops creates an infinite loop? Answer 1 for (var i = 0; i < 10; i++) { } Answer 2 for (var i = 0; i < 10; i--) { } Answer 3 for (var i = 10; i > 0; i--) { } Answer 4 All of the Above
I only know a tiny bit of code/coding logic but a for loop starts with an input (in this case, i = 0), applies a condition (in this case, i > 0 or 10 or whatever), and if the condition is met, it executes the following code (in this case, i-- or i++) so for the first example, it starts with i = 0, checks to see if i < 10, and if i is less than 10, it executes i++, which increases the value of i by 1. this keeps going as long as the condition is met. however, in code 1, i will eventually not be less than 10, so the loop terminates. that's why answer 1 is not correct, as this is not an infinite loop keep going with choices 2 and 3 and you'll see that one of them is an infinite loop b/c the condition is always satisfied
I got it. Thanks though
Join our real-time social learning platform and learn together with your friends!