Tuesday, July 23, 2013

For, Do & While

<script>
x = 1;
while(x < 10) {
document.write(x +" Mississippi<br>");
x++;
}
</script>

While loops and for loops do almost the exact same thing, but a for loop is ideal for a situation where you want your statement to run a set number of times, whereas a while loop can be set to run until it's conditional is satisfied: say for instance, you wanted to keep the a login window enloop until proper credentials were entered.

444444444444444444444444444444444444444444444444444444444444444444444444444444444

444444444444444444444444444444444444444444444444444444444444444444444444444444444

<script>
x = 0;
do {
document.write("do<br>wa<br>ditty<br>ditty<br>dum<br>ditty<br>doo<br>");
x++;
} while (x < 7);
</script>


A for loop will execute once, and check to see if the condition is met before executing again. Then it will continue to execute till the condition is through.

No comments:

Post a Comment