CHAPTER - 9

JavaScript Loops



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 the difference between While Loop and Do While Loop?

Everything is the same but the only difference is that the while loop condition is checked before while 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 because its starting from 10 and it is increasing by one then it cannot be less than 11

</script>

FOR LOOP

in the for loop, all the statements are nested at one place

<script>
for(i=0;i<=10;i++){
document.write(i+’<br>’);
}
</script>


Comments

Popular posts from this blog

CHAPTER - 14

CHAPTER - 18