Loops are essential when repeating a certain code multiple times. The loop will be repeated until a specific condition has been met. Loops are essential when trying to make a code simpler and more efficient. You do not have to repeat a certain code multiple times to get the action you want, the loop will do it for you. There are three different types of loops used in Javascript. The first one is a for loop, this loop repeats until a certain condition returns false. The for loop is the loop that we would want to use when wanting to run a code a specific number of times. The three components of a for loop are declaring and initializing a sum with a let statement at the beginning of your code, declaring and initializing a number in your for loop, and executing the adjustment statement. Once these three components have been met, the condition will return false once it has been satisfied. The second type of loop is a while loop. A while loop creates a loop that will only execute if the condition returns as false. A while loop would be the loop that you want to use when you are unsure of the specific number of times that the code will run. If the loop continuation condition returns true then the loop will continue. In a while loop, you have to initialize a count instead of a sum like in the for loop. Lastly, we have the do-while loop. The do-while loop also continues to run until the condition returns false.
Leave a Reply
You must be logged in to post a comment.