JS Loops!! Flashcards
1
Q
How “while” Loops work?
A
1.) Before each loop iteration, the condition in parentheses is evaluated to determine whether it’s true or not.
(The code associated with a loop is called its body.)
2.) If the condition's value is true, the code in the while loop's body runs. 3.) Afterwards, the condition is re-evaluated to see if it's still true or not. The cycle continues! 4.) If the condition's value is false, the code in the loop stops running or doesn't run.
2
Q
How a “for’” Loop works..
A
This is a little more complicated than the while loop syntax:
1.) Initialization only happens once, when the code first kicks off. It's often used to set the initial value of the variable associated to the loop condition. 2.) The condition is evaluated once before the loop runs each time. If it's true, the code runs. If not, the code doesn't run. 3.) The final expression is evaluated after the loop runs each time. It's often used to update the value of the variable associated with the loop condition.
3
Q
What is a Loop Counter?
A
The variable used during initialization, condition, and the final expression of a loop is called a counter and is often named i. This counter can be declared in the loop initialization to limit its scope to the loop body.