...

View Full Version : While Loops



ericd014
05-03-2007, 07:37 PM
Ok, I got great results last time I asked a question here. I hope it works again.
My assignment says this:
Write while loops to emulate each of these for loops:
1.

for (j = 4; j > 0; j --) {
document.writeln(j + "
");
}

2.

for (k = 1; k <= 99; k = k*2) {
k = k/1.5;
}

3.

for (num = 0; num <= 10; num ++) {
if (num == 8)
break;
}



I can do most the the work on this myself, but I have never been very good with loops...Can anyone help or offer advice?

felgall
05-03-2007, 07:58 PM
A for loop defines three values.

1. The value to set before starting the loop
2. The condition to test to continue looping
3. The value to change each time around the loop.

Set the first of these before the while loop, the second as the loop condition, and the third goes in the end of the loop.

liorean
05-03-2007, 08:10 PM
Pretty much it goes like this:
for(a;b;c){
d
}is equal to
a
while(b){
d
c
}

ericd014
05-14-2007, 07:11 PM
Ok, for some reason I still cant get it to work. Can anyone tell me what the code should look like or what would be displayed after the loop is ran.

Philip M
05-14-2007, 08:50 PM
Here is an example, but we cannot do your homework for you.

var i = 0;
var number = 50;
while (i < 10) {
number --;
i ++;
}

alert ("Loop stopped at iteration " + i + "\nNumber is now " + number);

ericd014
05-15-2007, 07:22 PM
Ok, I understand that you cant tell me how to do homework. Thats ok. But can you tell me if it is possibly right or way off? Because this is what I have so far:

j = 4
while (j > 0)
{
document.write(j + "<br>")
j--
}

It works, but that doesn't always mean it is correct...today seems to be a day to me that proves that... If you cant say correct or not, thanks anyways for all the help you have given me.

glenngv
05-16-2007, 12:39 AM
That's correct. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum