There's more than one way to do things PerlMonks

### Re: for loops

by xalphabetx (Initiate)
 on Mar 21, 2002 at 06:49 UTC ( #153256=note: print w/replies, xml ) Need Help??

okay so just trying to understand the logic of these new commands (for me) for(\$i=1; \$i<=100; \$i++) okay so now I understand how this line is read and the syntax but if the line starts over at the end doesnt \$i return to the value of 1? I know already that this code works obviously but howcome? in my brain it says it should be starting \$i back to a value of \$i=1 and the evaulating the rest again so I guess Im just not getting it any help??

Replies are listed 'Best First'.
Re: Re: for loops
by amarceluk (Beadle) on May 25, 2002 at 16:54 UTC
\$i++ is the equivalent of
```\$i=\$i+1;
where the = is an operator. It's not just sitting there, like it does in math, saying "these two things are equal"; it tells Perl to make the two things equal. So here it's saying "take the current value of \$i and add 1, and make that the new value of \$i." Then the for loop continues with the new value of \$i. This happens each time the loop runs, until \$i reaches 100.

_________
"Abby-somebody. Abby-normal."
Young Frankenstein
Re^2: for loops
by apotheon (Deacon) on Oct 11, 2004 at 01:02 UTC
On second and following iterations, the for loop ignores the initial assignment of value to \$i and uses the incremented value (or decremented, if you use a decrement operator instead of an increment operator) instead. The \$i=1 assignment happens once, and once only, in for(\$i=1; \$i<=100; \$i++).

- apotheon

Create A New User
Node Status?
node history
Node Type: note [id://153256]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-05-28 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
If programming languages were movie genres, Perl would be:

Results (165 votes). Check out past polls.

Notices?