DEV Community

R.Shobika CSE
R.Shobika CSE

Posted on

LOOPING PART - 2

FOR LOOP

For loop is also similar to the while loop. In while loop the we have to give the initialization, Condition, Increment/ Decrement in a multiple line but in for loop we can give these all things in a single line .

SYNTAX

for(initialization, condition, increment/decrement){
-----------
-----------
}
Enter fullscreen mode Exit fullscreen mode

Example Program

for(let i=1;i<=5;i++){
console.log(i);
Enter fullscreen mode Exit fullscreen mode

output:
1
2
3
4
5

Not only compulsory we should have to give in the single line we can also give like this also

let i=1;
for( ; i<=5; )
{
console.log(i);
i++;
}
Enter fullscreen mode Exit fullscreen mode

output:
1
2
3
4
5

Both the for and while are called as aentry check

*do while *

The do...while statements combo defines a code block to be executed once, and repeated as long as a condition is true.

SYNTAX

do{
------
------
}while(condition)
Enter fullscreen mode Exit fullscreen mode

EXAMPLE PROGRAM

let i=5;
do{
console.log("Hello");
i++;
}while(i<5)
Enter fullscreen mode Exit fullscreen mode

output: Hello

The do while loop is called as a exit check

Top comments (0)