what are loops?
Imagine you have a problem statement where you have to run a group of statements continuously until a particular condition is met. Now imagine doing this task without loops.
isn’t that nearly impossible, and so it is important to know, understand, and effectively use loops while coding.
With loops, we can repeat parts of code. Most of the time this means that we repeat some kind of instructions for different cases.
How do you know which one to choose, and when? In this article, we are going to cover the most popular loops telling you when and why you would use each. By the end of the article, you will be sure of which loop to use.
We repeat the sequence of instructions until the test condition is true. The different looping statement available are:
- Do while
- For in
- For of
All the types have their own purpose and I will explain them one by one, later on, we also have an example you can use on a simple web page.
This is the most common and the oldest loop. A for loop repeats until a specified condition evaluates to false.
For loop has 3 parts: initialization, condition, and final-expression.
Initialization: we set the starting value for our loop variable. This is a variable that should change with every iteration. Many times i is used as a name for the variable. but we recommend you to use more meaningful values like count,…. In fact, a name that makes your code more readable.
Condition: It specifies a certain condition in the loop that determines whether the loop body will execute or not. If the condition returns true, the code inside the for loop will execute. If it returns false, the control moves onto the statement after the loop.
Final Expression: This is the statement that will run at the end of the loop, after each iteration of the loop. Final-expression is in most cases some kind of increase/decrease of the loop variable.
The conditional expression is executed every time the loop runs and if it evaluates to true then the statement inside the loop gets executed else the loop terminates. Also if the conditional expression is not there then it is considered as true by default.Increment/decrement expression executes once the statement inside the loops gets executed and updates its value. Statements inside the loop execute only when the conditional expression returns true as mentioned above.
This loop is useful when you know how many times you want the loop to execute. This loop also reduces the total lines in your code to achieve the same task.
A While Loop will continue to loop while a condition is true. It has only “one” part, condition. A lot of times the value for the condition is changed within the loop itself.
You have to be careful with the while loop, since a lot of times, developers make mistakes and the condition will never fire, this means that loop never ends. This kind of code then makes an application unresponsive, the code is without error, but this is a runtime error and hard to find. To prevent this kind of error, you can add a counter, that breaks the loop after a number of loops.
For each loop, we check the condition (noFox) and if it is “true”, then the loop continues.
While loop can be used for:
- Waiting for a condition to be true
- Increasing values till we reach some undefined value
- Searching for the end of the (data) tree
Do while loop
This loop is almost identical to the while loop, except the condition check is at the end.While and Do while loops are very similar.with one more important difference do while will run at least once and it will run until the condition is true.
The break statement is used for jumping out of a loop. It will help you in breaking the loop and continue executing the code after the loop.
Here once the “i” value becomes 7, the loop directly terminates and continues to execute the code followed by for loop. And the output obtained here is 0,1,2,3,4,5,6.
The continue statement breaks one iteration in the loop if a specified condition occurs, and continues with the next iteration in the loop. The difference between the continue and the break statement is that the continue statement “jumps over” one iteration in the loop instead of “jumping out”.
When the continue statement gets executed it skips the rest of the statements to be executed in the inner loop and jumps back to the main loop and then continues the next iteration.
So here in the example where i = 3 the control skips the next console statement and continues with the next iteration. The same code without a continue statement gives the 1,3,6,10,15 as an output.
For in loop
For in is very helpful if we have an object we know nothing about, and this enables us to shift through properties and make decisions. This happens a lot with server calls. Also, this makes using prototypes easier since we can make a loop not knowing all the properties.
For of loop
For of loop iterates through objects.The sole difference between for..in and for..of loop is that for…in iterates using properties of an object while the for..of iterates using properties values.
Here the iteration is on the values instead of indexes The difference between for in and for of can be seen with the below example where output obtained is indexes of the array instead of values.