When we have to do the same task again and again with a slight difference in pattern, it’s frustrating…! developers are considered to be one of the laziest community in the world. As a developer no one can dream even to code without loops, it is one of the most important pillars of programming, in any language.
Imagine you have a problem statement where you have to run a group of statement continuously until a particular condition is met. Now imagine doing this task without loops.
Uff isn’t that nearly impossible, and so it is important to know, understand and effectively use loops while coding.
In general coding practices, we have multiple different types of loops available which serves the same purpose that is repeating the same task for multiple numbers of times.
Also, it is important to remember that the number of repetition could be zero if there is no condition met.
And these different looping statement provides us different ways to find a start point and endpoints. The different looping statement available are
For LOop: for loop keep executing the given statement until the conditional expression met to false.
[Read: React-Router: Let’s unwind another mystery in React]
Do while LoOp: Do while loop executes the statement at least once no matter what and keep executing the statements until the while expression is set to false.
If the condition returns true then the loop continues to run and executes the statements.
Here in the example, the expression will return false but still the statement will be executed for the very first time.
While LoOP: While loop is very similar to do while. Except for the one thing, that the statements inside the loop only get executed once the conditional expression returns true.
The example mentioned here is similar to the example mentioned in do-while. But here output will be different since the condition returns false in the first place so the statements inside the loop will not be executed even once.
Labeled statements: Labeled statement are used for any loop and later can be identified anywhere in your program.
Here labels can be any Javascript identifier and not any reserved words.we can also use a break and continue statements inside the loops to break or continue the execution of the loop.
Continue statements: continue is a statement and not a loop but widely used to control the execution of loops. It can be used inside any for, while or do-while loops.
I have taken a very interesting example to explain the working of continue from MDN official site as given below.
Here the output obtained is 1,3,7,12.
When continue statement gets executed it skips the rest of statements to be executed in the inner loop and jumps back to the main loop and then continue the next iteration.
So here in the example where i = 3 the control skips the next console statement and continues with next iteration.
Same code without continue statement gives the following output.
Output:
1,3,6,10,15
[Read: NodeJs Framework: What’s new in web frameworks in 2019]
Break statement: Break statement is used to break the loop, switch or any labeled loops. Once the break statement executes the rest of the code inclosed inside the loop will be skipped.
Here once ” 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.
For…in LoOps: for…in loops are used for iteration through distinct properties of an object. So for each distinct property, the control executes the given statement.
Where the output is
The value for the Key name is Roshni
The value for the Key age is 22
The value for the Key gender is female
The value for the Key designation is Developer
For..of LoOp: The for..of loop is used to loop through the property value of an object. 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.
Output is
Roshni
22
female
Developer
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.
Where the output obtained is 0,1,2,3.
[Read: Video Analytics – Object Extraction by background subtraction and noise filtering]
If you’ve any doubts, please let us know!!
Be it a software developer, programmer, coder, or a consultant, CronJ has it all. CronJ has been a trustworthy company for startups, small companies, and large enterprises. Hire the web of experienced ReactJS Development Services for your esteemed project today.
Follow Us on Facebook | Twitter | LinkedIn.
Let CronJ assist you..!
Thank You!!!