Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37318
| From | Jon Ribbens <jon+usenet@unequivocal.eu> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: What does this line of code mean? |
| Date | 2019-09-21 18:30 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnqocr2r.5c3.jon+usenet@sable.unequivocal.eu> (permalink) |
| References | <f27ce718-9b0b-4641-86f1-bfc079462875@googlegroups.com> |
On 2019-09-21, groovee@cyberdude.com <groovee@cyberdude.com> wrote:
> My JS book was explaining Graphs, and in the code for "finding shortest paths using BFS", it had this line:
>
> blah blah..
> for (var v=toVertex; v!== fromVertex; v=shortestPathA.predecessors[v] {
> path.push(v);
> }
> blah blah...
>
> What is the "for" line doing? I'm especially not understanding the
> THIRD argument - I always thought the 3rd thing in a "for" HAD to be
> "something++" or "something--"?? Could someone please explain?
No, the third argument (indeed all the arguments) can be any
expression. The first one is always evaluated once, the second one
is evaluated each time before the code inside the 'for' statement
is executed and causes the loop to immediately terminate if its
result is a false value, and the third one is executed each time
round the loop after the inner code has run.
So "for (a; b; c) { d }" is similar to: "a; while (b) { d; c }".
All of the expressions are optional, for example "for (;;) { ... }"
is a simple way of creating an infinite loop (although the loop can
of course be terminated by the code inside it executing a 'break').
As another example, you might walk along a linked list of items with
something like: "for (let item = listHead; item; item = item.next)".
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What does this line of code mean? groovee@cyberdude.com - 2019-09-21 11:15 -0700
Re: What does this line of code mean? Jon Ribbens <jon+usenet@unequivocal.eu> - 2019-09-21 18:30 +0000
Re: What does this line of code mean? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-09-22 11:25 +0100
Re: What does this line of code mean? Julio Di Egidio <julio@diegidio.name> - 2019-09-22 04:53 -0700
Re: What does this line of code mean? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-09-22 15:40 +0100
Re: What does this line of code mean? "R.Wieser" <address@not.available> - 2019-09-22 19:01 +0200
Re: What does this line of code mean? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-09-23 11:13 +0100
Re: What does this line of code mean? "R.Wieser" <address@not.available> - 2019-09-23 14:15 +0200
Re: What does this line of code mean? groovee@cyberdude.com - 2019-09-25 12:20 -0700
Re: What does this line of code mean? "R.Wieser" <address@not.available> - 2019-09-22 15:39 +0200
Re: What does this line of code mean? Julio Di Egidio <julio@diegidio.name> - 2019-09-22 06:57 -0700
Re: What does this line of code mean? "R.Wieser" <address@not.available> - 2019-09-22 18:49 +0200
csiph-web