Chapter 9 - Loops
Chapter 9 - Loops
OBJECTIVES:
➢ Learn what loops are
➢ Learn the different kinds of loops available in JavaScript
➢ Learn how to use loops
What is a Loop?
There are times when you want a block of code repeated over and over again until a certain condition is
satisfied. This can be done by creating a loop. A loop is a repetitive cycle of a block of code until the
condition is met.
❖ For loop – it loops through a block of code within a specified number of times.
❖ While loop – it loops through a block of code until the condition is satisfied.
❖ Do-while loop – it is like while loop, however, it will execute the set of codes at least once
❖ For-in loop – it loops through the elements of an array.
If for instance, you want to exit from the loop after a condition has been met, you can use the break
statement. This statement will immediately break the loop and continue the execution of codes after the
said loop. The continue statement will break the current loop and will continue to the next value.
}
Syntax of a while loop while (condition)
Video Links:
https://www.youtube.com/watch?v=24Wpg6njlYI
https://www.youtube.com/watch?v=DIA0J4vJBHQ
References:
https://www.w3schools.com/js/js_loop_for.asp
https://www.w3schools.com/js/js_loop_while.asp
https://www.w3schools.com/js/js_break.asp