Loops Flashcards
What is a loop?
A loop is a snippet of programming that repeat actions, in specific conditions, until a condition is true.
What are examples of a loop?
Repeat same set of actions, over and over again
Examples:
- create a list of 1000 numbers
- display 20 photos
- repeat prompt dialog until correct answer
What happens without using loops?
Without loops, you have to write code manually.
No big deal when it’s a small amount, but it is repetitive.
It’s harder for fellow developers.
Why use loops?
Loops help automate code.
It’s better to automate code than duplicate code.
Repetitive code doesn’t scale well for 100 or 1000 times.
What analogy can you make to compare the concept of loops to understand what it does?
Imagine you’re running a marathon, 26.2 miles
Path 1 is random, unpredictable, confusing, inefficient
Path 2 is a well-defined loop, consistent, predictable
The loop is like the second path efficient & effective.
What are the 3 main types of loops?
While
Do While
For Loop
What is the syntax for the while loop?
while () {
}

What is the tell-tell sign you should use a loop?
When you have duplicates, copy/paste, or repetition of manually written code, then it makes sense to create a loop to write efficient code.
Explain the logic behind this while loop example
line for line

the function generates the random number
lap counter, at the start it’s 0
the console.log prints the message with a random number.
Each time the loop runs, it adds 1 to the counter
JS interpreter asks “Is the counter less than 10?”
It runs again and again.
The counter runs 10 times, producing 10 random numbers.
What is the do while syntax?
do {
} while ( );

What’s the difference between the Do Loop and Do While Loop?
The do while will always execute code, at least once, before the condition is checked
The loop will check the condition first, before running the code.
When should you use a loop?
When you want to repeat the same set of
actions a certain number of times.
Fill in the blanks provided below

let
while

How many times does a loop repeat?
A loop repeats an action over and over again, or a given number of times, as long as a test condition evaluates to true.
Please fill in the correct answer in each blank provided below.
Complete the code below so that the loop runs 10 times.

counter < 10

Why will the following loop never run?

The variable num begins with the value of 0.
The condition asks if that variable is greater than 20.
0 is not greater than 20, so the loop never runs.
While loop runs a block of code before checking the condition?
Do While
Enter the correct answer in each blank provided in photo

do
while

What is an increment operator?
An increment operator increases a number value by 1
How do you use the increment operator?
Use the increment operator by replacing
+=1 with two plus symbols (++)
Where is the increment operator in this example?

counter++;

What’s the logic behind the increment operator?
It’s like using the addition operator and adding 1,
the incrementor operator adds 1 to the current number stored in the counter and returns a value each time through the loop.
What does the decrement operator do?
(–)
It subtracts 1 from an integer value.
What happens in this example with the decrement operator?

This time, the loop counts down from 10 to 0.
Create a while loop that logs the current value of count to the console 26 times.
Use the count variable to track the number of times the loop runs.
Don’t forget to increment count by one each time through the loop.

You don’t always need to use a counter or specify an exact number of times that a loop must run. All you need is a condition that evaluates to false at some point so that the loop can end.
The code in script.js opens a prompt dialog that asks for a password and assigns it to the variable secret. It also displays an alert dialog. Currently, no code checks the password.
Add a do…while loop that keeps displaying the prompt dialog until the user types ‘sesame’.

What is an infinite loop?
An infinite loop keeps running forever.
What helps you exist a loop?
Break helps you exit a loop.
Why is this example an infinite loop?

The counter starts at 0 and increases each time through the loop. The counter will always be greater than or equal to 0, the condition will ALWAYS be true. This is why it is an infinite loop.
What happens if you create an infinite loop?
How do you prevent infinite loops?
Establish a condition that eventually becomes false.
What is the consequence of an infinite loop?
Infinite loops crash browser or computer.
Why is this an infinite loop?

The counter will always be greater than 0. This is why it’s an infinite loop.
1
Please fill in the correct answer
Consider the following console output:
Complete the do… while loop to display the above output to the console:

Given the code below, what will display in the console?

Hello Goodbye
Do you always need to use a counter to specify an exact number of times a loop must run?
Please fill in the correct answer in each blank provided below
Complete the code including shorthand operator that increase the counter by one each time through the loop
++

Given the code below, what will print to the console?

The condition is false, the loop never runs and Hello never prints.
Why is this foor loop shorter?

It has 2 lines of code, instead of 3.
What does the counter do?

The counter is evaluated before the loop begins.
Is the counter condition evaluated before the loop begins each time?

Yes
The counter needs to be below 10 each time before the loop begins.
counter < 10;
How is the counter being updated with each loop iteration?

counter +=1;

How would you incorporate the increment operator into this example?

Use counter ++;
Why would you use letters for variables?
Make loops even easier to write with single letters than whole words
What does the letter i generally substitute in the loop?
i replaces counter mentions in the loop
you can use letters for counter variables

Aside from i, what other letter is commonly used as a single letter variable name?
J
Create a for loop that logs the numbers 5 to 100 to the console. Use the console.log() method to log a value to the console.

What is the outcome produced by this loop?

It produces 10 divs.
Why is the greater than or equal to sign important?

It ensures that the numberes in the div is
truly 1 through 10, rather than 1 through 9.
Can the counter iterate by different multiples? Or is it stuck to just 1?
You can do multiple of 5s or any number by changing the skip pattern
What’s the multiple by which the iteration moves in this for loop?

Increases by 5
i += 5
What are the two ways to exit a loop?
It can be done through counter conditions.
It can be done through adding a break statement.
What is a break statement?
The break statement immediately exits the
loop, when encountered inside a loop.
In this example, when is the break statement applied?

The break is triggered when the guess matches the secretWord
Does code after the break statement run?

The break statement works like a return statement.
Any code inside the loop that appears after the break statement will never run
The for loop in script.js runs as many times as the length value of the string assigned to the message variable. It logs the current value of i to the console, and the rest of the program continues when the loop completes.
Add the statement that immediately terminates the for loop if the value of i is equal to message / 2 (half the length value).


What does refractor mean?
It means to make code more succinct.
What does D.R.Y. mean?
Don’t repeat yourself
Why is D.R.Y. one of the most important
concepts a programmer can learn?
Reduces repetition of code
How is D.R.Y. code beneficial?
Easier to read
Easier to maintain
What’s a tell-tell sign you need to use refractoring?
When you have copied/pasted code again and again or
you have lots of repetitive sets– time to use a loop to condense
Consider the following code example
Why is this NOT an infinite loop.

It eventually ends when the counter is less than 100.
Please fill the correct answer in each blank space provided below
Complete the code below to create a for loop that executes 10 times, logging the value of the variable i to the console each time:

for
10

Which word forces the JS engine to exist a loop even when the loop condition is still true?
break
How many times will the following loop run?

Actually 11
BEcause it starts at 0, not 10.
What’s the shorthand version of the following code?


What causes an infinite loop?
When the loop’s condition never evaluates to false.
Refractor the code to be more concise

