For Loops

G

Gregc.

Hi

I am not sure you guys can help me, but here it goes. am having
trouble understanding for loops. If I have a loop that says:


var coffee = new Array();
coffee ["mixedblend"] = 5.50;
for (c in coffee)

{code goes here}

That for saying while there is a c in coffee, then conduct the code.
Is that a correct interpretation?

Greg
 
R

Randy Webb

Gregc. said the following on 8/29/2006 8:27 PM:
Hi

I am not sure you guys can help me, but here it goes. am having
trouble understanding for loops. If I have a loop that says:


var coffee = new Array();
coffee ["mixedblend"] = 5.50;
for (c in coffee)

{code goes here}

That for saying while there is a c in coffee, then conduct the code.
Is that a correct interpretation?

Yes.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 29 Aug 2006 17:27:07 remote, seen in
news:comp.lang.javascript said:
I am not sure you guys can help me, but here it goes. am having
trouble understanding for loops. If I have a loop that says:

var coffee = new Array();
coffee ["mixedblend"] = 5.50;
for (c in coffee)

{code goes here}

That for saying while there is a c in coffee, then conduct the code.
Is that a correct interpretation?

No.

Consider and try the following

var coffee = new Array();
coffee ["mixedblend"] = 5.50;
coffee ["grots"] = 1.50;
for (c in coffee) alert (c + " costs " + coffee[c])

IMHO, it is generally better to use Array only for numeric indices, and
the above could start var coffee = {} // empty Object.

The ordering apparent in coffee by for (c in coffee) is undefined.

You should use var c; and, as that is essentially an indexing operation,
I'd use J instead of c.
 
G

Gregc.

Dr said:
Consider and try the following

var coffee = new Array();
coffee ["mixedblend"] = 5.50;
coffee ["grots"] = 1.50;
for (c in coffee) alert (c + " costs " + coffee[c])

IMHO, it is generally better to use Array only for numeric indices, and
the above could start var coffee = {} // empty Object.

The ordering apparent in coffee by for (c in coffee) is undefined.

You should use var c; and, as that is essentially an indexing operation,
I'd use J instead of c.
Hi

what does IMHO mean?

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

For loops... 2
loops 1
Geo cache puzzle 7
Help with variables and 'for' loops 2
Padding strings for a clean visual print out... 5
Bash scripts for web apps 1
function in for loops 4
fun with nested loops 14

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top