One Pager Cheat Sheet
- By leveraging
loops
anditerations
, we can automate the process of applying a similar operation to a collection of elements in a program. Iteration
andloops
are programming structures that repeatedly execute a set of instructions, whileiterables
are objects, such asarrays
andstrings
, that can be accessed one element at a time.- Loops are used to iterate over a collection of objects, with
for
andwhile
loops being the most common types found in most programming languages, such as JavaScript. - In JavaScript,
For
loops are used to iterate over a sequence of elements by specifying three statements: a variable initiated before the loop starts, a boolean condition to be checked each time, and a value change to take place every time the code block is executed. While
loops are used to iterate over a sequence of elements, while a condition remains true, and they require initialization before the loop.- The loop will run while
i
is lesser thanfruits.length
, which is currently6
, because thelast element
was removed usingfruits.pop()
. - The
for loop
syntax requires the comparison to compare the iterator variable with thearray length
, a number value, but the code uses thearray
numbers
which will result in an error. - One must be aware of the
break
,continue
, andinfinite loop
keywords when working with loops to ensure the program does not execute indefinitely. - The code will print all letters except for "o",
skipping
over it withcontinue
and still printingp
. - This program is an example of an infinite loop, since there is no code to change the value of
a
, and the loop condition ofa <= 5
will always remain true. - Iterations (or
loops
) help us reduce lines of code, improve code reusability, and enable us to specify the number of times a code block needs to be repeated.