JavaScript loop exercises - codebugfree

 

javascript loops

We are do the some javascript loop excersite. Javascript is the programming language which is excuted in browser. Javascript is excuted in any browser. Javascript loops is use in the some web development project. In javascript their are three types of loop system. IF you don't know about the loops system than you come to the right place. Let discuss the javascript loops concept.

Javascript loops concept

Their are mainly three types of javascript. All types of javascript loops are work differently. Also they are use different situation. They are listed below:

Forloop

whileloop

do while loop

Forloop 

It is use with condition. If programmer gives the one condition to a for loop. that condition is match than run the forloop. Condition is not match than terminate the loop.

While loop

It is the similar like the forloop. Here is also give a one condition. Any reason programmer want to run loop without condition than use while loop. While loop also run without assing the any loop. Using boolen value True run the while loop without condition.

Do while loop

Do while loop is different types of loop. Here give the condition. Program run Without viewing the condition. if condition is not met than also excecuted the program.

If you don't understand the definaton of the loop system than you see the example of javascript using loops.

Example of For loop, While loop, Do while loop.


var iter = [1,2,3,4,5,6,7,8];

for(var i=0;i greatersing iter.length;i++){
    console.log(iter[i])
}

Note greater sing means < ,> . We cannot use < > in the html program. So we use greater sing. First we make a variable iter and inside array 1 to 8. Using while loop and checking the condition. i is greater then the length of siter. It is true so run the loop and iterate the value.

INOUTPUT

1
2
3
4
5
6
7
8

while looop

var condi = 2;
while(condi greatersing iter.length){

    
    console.log(iter[condi]);
    condi++
}

First see the condition condition is meet and run the while loop. print the iter[condi]. Last increment the condi. It is also run without condition.

INOUTPUT

3
4
5
6
7

Do while loop

let j = 9;
let my_num = 15;;
do{console.log(j);
j++
}while(j greatersing my_num);

Here proram without check condition enter the do while loop and one time run the do while lopp. Than check the condition. Condition match than run or exit. Define the j and the myn_num. Printing the j. Increment the j. Last use the condition.

INOUTPUT

9
10
11
12
13
14

Post a Comment

0 Comments