A loop allow to execute one or many statement repeatedly until the condition is satisfied.
1. for Loop : it will iterate until i<5 condition get failed.
example code:
1 2 3 4 |
//for loop for(let i=0;i < 5; i++){ console.log(i) } |
output:
0
1
2
3
4
2. do while: execute statement and then check for condition. if fails, it will exit the loop.
example:
1 2 3 4 5 |
let x=0; do{ x++; console.log(x) } while(x < 5) |
Output:
1
2
3
4
5
3. while : first check and then execute the statement
1 2 3 4 5 |
let y=0; while( y<5){ y++; console.log(y) } |
Output:
1
2
3
4
5