0
0
Javascriptprogramming~5 mins

For loop in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a for loop used for in JavaScript?
A for loop is used to repeat a block of code a specific number of times. It helps automate repetitive tasks.
Click to reveal answer
beginner
Identify the three main parts inside the parentheses of a for loop: for (___; ___; ___)
The three parts are: 1) Initialization (sets a starting point), 2) Condition (checks if the loop should continue), 3) Update (changes the loop variable each time).
Click to reveal answer
beginner
What will this loop print?<br><pre>for (let i = 0; i &lt; 3; i++) { console.log(i); }</pre>
It will print:<br>0<br>1<br>2<br>Because it starts at 0 and runs while i is less than 3, increasing i by 1 each time.
Click to reveal answer
intermediate
How can you stop a for loop early?
You can use the break statement inside the loop to stop it immediately when a condition is met.
Click to reveal answer
intermediate
What happens if the condition in a for loop is always true?
The loop will run forever, causing an infinite loop, which can freeze or crash your program.
Click to reveal answer
Which part of the for loop controls how many times the loop runs?
AThe loop body
BThe initialization
CThe condition
DThe update
What does this loop do?<br>
for (let i = 5; i > 0; i--) { console.log(i); }
ACounts up from 5 to 0
BCounts up from 0 to 5
CCounts down from 0 to 5
DCounts down from 5 to 1
What keyword stops a for loop immediately?
Abreak
Bstop
Ccontinue
Dexit
What will happen if you forget to update the loop variable in a for loop?
AThe loop will run once
BThe loop will run infinitely
CThe loop will not run
DThe loop will run twice
Which of these is a valid for loop header?
Afor (let i = 0; i < 5; i++)
Bfor (i < 5; i++)
Cfor i = 0; i < 5; i++
Dfor (let i = 0; i++)
Explain the three parts of a for loop and how they work together.
Think about how the loop starts, checks if it should continue, and changes each time.
You got /4 concepts.
    Describe a real-life situation where a for loop could help automate a task.
    Imagine doing something many times, like counting items or sending messages.
    You got /4 concepts.