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.