Hi all, today we are going to learn about loops in JavaScript, another important topic.
Programmers are like to work smarter not harder. So think that if you have to write same line again and again, what you would do. It is a burden, isn't it? So, at this point loops come to our help.
What is a loop?
Loop allows to run code repeatedly as many time that we wants without having to write that line of code again and again. If we want to write something 10 times, we can do it using loops easily.
In this tutorial we are going to discuss about four loops and two statements which are used in loops. They are
- while loop
- do-while loop
- for loop
- for-in loop
- break statement
- continue statement
while loop
In while loop, condition will be executed over and over again until the condition or expression becomes false. Once the expression becomes false, the loop terminates. Every time loop does that calls an iteration. There is one problem of while loop. What happens if conditions never fail, so, it called infinite loop. It is bad. Because only way we can stop the program in here is through task manager. Make sure that you never have a value, that never reach false to avoid this.
Figure 1
Figure 1 shows how while loop works.
Syntax
while (condition){
Statement(s) to be executed if condition is true
}
Let's look at example and try to understand this.
Figure 2
We have to use a variable to count how many times this loop runs. In here we use i. First, variable i is assigned to 1. This means we start to count the while loop at 1. Condition involves i. Then we must increase the counter inside the loop. The program carry on going until we get 11. When we get 11, condition become false and code is not run and stopped the program.The result is shown in Figure 3.
Figure 3
do-while loop
This is similar to while loop. But, in here, condition is checked at the end of loop.
Figure 4
Figure 4 shows the flow chart of do-while loop.
Syntax
do{
Statement(s) to be executed;
}
while (condition);
Example 2
Figure 5
Output is shown below.
Figure 6
For Loop
For loops avoid making infinite loops by accident. This includes built in counter. Variable is increment every time loop reiterates or restarts. We don’t have to declare variable separately and increment inside the loop. For loop does all this. Figure 7 shows the flow chart of for loop.
Figure 7
Syntax
for (initialization;condition;increment){
Statement(s) to be executed if test condition is true
}
In for loop there are 3 separated statements. Initialization declare counting, condition is to find the end of for loop and increment is to increment the counting.
Example 3
Figure 8
In here first, i is assigned to 1. Then check whether i is equal or less than 10. Since the condition is true, the line inside the block is executed. At the end of loop, increment takes place. Then loop starts with i=2 and same thing happen again. when i=11, since the condition is false, go out from the loop.
Result is follows.
Figure 9
For-in loop
A for-in loop iterates through the properties of an object and executes the loop's body once for each enumerable property of the object. You'll not be very clear about this because you haven't any experience of objects. I'm going to do my next tutorial about objects. I'm pretty sure that after you study that tutorial you'll clearly understand about this.
Syntax
for (var variablename in object){
statement(s) to be executed
}
Example 4
Figure 10
In here, there are three properties for the object called 'person'. They are first, last and age. Values are assigned to these properties. For-in loop iterates through all these three properties and concatenate them and assigned to the variable 'me'. Here is the result we got.
Figure 11
break Statement
When JavaScript encounters a break statement in a loop it immediately exits the loop without executing any other statements in the loop.
Example 5
Figure 12
In here, since while loop is in here, loop must iterate 9 times(since it is checked, whether x is less than 10). But, there is a break statement. so, when x gets equal to 5, loop immediately exists. So here is the result we got.
Figure 13
continue Statement
When JavaScript encounters a continue statement in a loop it stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.
Example 6
Figure 14
In here, since this is for loop values from 0 to 9 must show continuously. But since there is a continuous statement for i=5, when i equals 5, it stops the execution of current iteration and begin the next iteration that means 6.
So, the result is follows.
Figure 15
Hope you will understand loops from this tutorial. Go through examples carefully. Thank you for watching this tutorial.