Javascript Loops
How many values can you initiate in statement 1 of a for loop?
As many as you want.
When is statement one executed in a for loop?
Before the loop (code block) starts
What is a while loop?
Can execute a specific code as long as condition as true.
What is statement two in a for loop?
Defines the condition for running the loop (code block)
When is statement three in a for loop executed?
Each time after the loop (code block) has been executed
How does statement 2 of a for loop relate to a boolean?
If statement two returns false, it will end the loop. If it returns true, it will keep going.
What is a for/in loop?
It loops through the properties of an object.
Does statement 3 always need to count up in a for loop?
No, it can count down (i--) or perform a different function. It can also be omitted entirely if the increment is defined in the loop.
What does i++ mean?
This increases the value returned by one on each execution.
For Loop: Usage
To run the same code many times and gather a different result each time.
Can you omit statement 1 in a for loop?
Yes, like when your values are defined before the loop begins.
How do you avoid an infinite loop if you omit statement 2?
You must provide a break in the loop.
Do/While Loop: Syntax
do { code block } while (condition);
Do/While Loop: Example
do { text += "The number is" + i; i++; } while(i < 10);
For loop: Syntax
for (statement 1; statement 2; statement 3) { code block to be executed }
For Loop: Examples
var define = boolean; for (i=0, i < variable or number, i++) { //execute code here console.log("Some text" + i + "some more text <br/>"); }
For/In loop: Syntax
var person = {fname:"John", lname:"Doe", age:25}; var text = ""; var x; for (x in person) { text += person[x]; }
While loop: Syntax
while (condition) { //code to be executed }
While loop: Example
while (i < 10) { text += "The number is" + i; i++; }