Help FOR me?


#1

Can someone explain the for loop? I never got it explained and I'm lost in the dark with it. :confounded:


#2

Basically, all of the code inside a for loop keeps on running until the condition, given at the start of the loop in the brackets, is no longer satisfied.

for (i = 0; i <10; i++) {
console.log("Loop the loop!");
}

The above code will repeat the code inside until the condition is met: As long as i is less than 10, it will run the code in the loop, and each time the code is completed, i gets increased by 1. This results in "Loop the loop!" being logged to the console 10 times.


#3

Yup, you can also do something like this to demonstrate the loop. It's an audible interaction.

import winsound
from time import sleep

for i in range(50):
    winsound.Beep(i + 50, 100)
    sleep(1)

#4

Thanks! I never really understood for, but this clears things up a whole lot! :smile: