Course Banner


Loop

Loop mainly contains three parts.
1.Initialization
2. Condition
3.Increment or decrement
WHILE LOOP
<script>
var i=0; // initialization
while(i<10){ // condition
document.write(i+’<br>’);
i++; //increment
}
</script>
DO WHILE LOOP
What is difference between While Loop and Do while Loop ?
Everythign is same but only difference is that in the while loop condition is checked before while in the Do While Loop condition is checked later. So, even the condition is false in the case of Do While Loop at least once the program executes.
<script>
var i=11;
do{
document.write(i+’<br>’);
i++;
}while(i<11);
// the condition is false becuase its starting from 10 and its increasing by one then it cannot be less than 11
</script>
FOR LOOP
in for loop all the statements are nested at one place
<script>
for(i=0;i<=10;i++){
document.write(i+’<br>’);
}
</script>