We use loops when we want code to run more than one time. For loops run for a certain number of times specified at the beginning. While loops run for as long as a condition is met (not a specific number). For example, “for” loops are great for running through a list of a certain number of objects, while “while” loops are great for performing a task while a certain condition is true. When we create a for loop, it must have three parts: the keyword that starts the loop, the condition being tested, and the end keyword that terminates the loop. An example of the syntax for a “for” loop would be:
for (let days = 1; days < 8; days++){
console.log(“Today is the “ + days + “ day”);
}
We are starting at days = 1 and printing a statement for each day. After the statement is printed, the number of days is incremented and the loop starts over. When we reach days = 8, the for loop stops and the condition is not executed since it only executes when days is less than 8. In total, the loop will run 7 times. We separate the variable ‘days’ with a “+” sign since we want it to print the value of the variable, not the word “days”. Anything printed between quotation marks will be printed as a statement and not a variable.
Hi Sophie! I enjoyed reading you describe loops, they can be a bit complicated, but you provided examples and clearly explained it as well! Great job! Great read!