"for" loops, I don't get them!
- Cheesemold
-
Cheesemold
- Member since: Mar. 16, 2001
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
could someone please explain those "for" loops to me? I'm mainly wondering about them in Perl, what does for ($i = $#banners; $i >= 0; $i--) ?? Thanks.
- thisisnotmike
-
thisisnotmike
- Member since: Aug. 26, 2000
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
They work the same in perl as in php and javascript and asp, but I'm not sure how perl variables are, so bare with me:
for($n = 0; $n < 10; $n++)
{
echo($n);
echo("<BR>
");
}
That would display:
1
2
3
4
5
6
7
8
9
10
For works like this. It performs what you tell it to on a given value until it equals another value (unless it is infinite).
Like this:
for(value, condition to be met, operation to be performed)
{
The crap you want it to do.
}
Sorry if my explaination of it is unclear. I had a bit of trouble with for at first, too.
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
At 7/25/01 12:40 AM, cheesemold wrote: could someone please explain those "for" loops to me? I'm mainly wondering about them in Perl, what does for ($i = $#banners; $i >= 0; $i--) ?? Thanks.
for (initial expression; termination check; loop end expression) {
// do stuff
}
-- increments a variable by -1
++ increments a variable by +1
So, in your statement, while the variable $i (number of banners) is greater than or equal to 0, keep counting down to 0 ($i--).
- Pecos
-
Pecos
- Member since: Dec. 29, 1999
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
At 7/25/01 07:22 AM, liljim wrote:At 7/25/01 12:40 AM, cheesemold wrote: could someone please explain those "for" loops to me? I'm mainly wondering about them in Perl, what does for ($i = $#banners; $i >= 0; $i--) ?? Thanks.for (initial expression; termination check; loop end expression) {
// do stuff
}
-- increments a variable by -1
++ increments a variable by +1
So, in your statement, while the variable $i (number of banners) is greater than or equal to 0, keep counting down to 0 ($i--).
I'm not sure how it works in PHP, but as far as C/C++ and Java go, this is also an interesting point to consider.
i++ and ++i might give you different results. let's say you do this:
int i = 5;
int x = i++;
Then x is set to 5 before i is incremented. But if you do:
int i = 5;
int x = ++i;
Then x is set to 6, because i is incremented first.
- Pecos
-
Pecos
- Member since: Dec. 29, 1999
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
At 7/25/01 07:22 AM, liljim wrote:At 7/25/01 12:40 AM, cheesemold wrote: could someone please explain those "for" loops to me? I'm mainly wondering about them in Perl, what does for ($i = $#banners; $i >= 0; $i--) ?? Thanks.for (initial expression; termination check; loop end expression) {
// do stuff
}
-- increments a variable by -1
++ increments a variable by +1
So, in your statement, while the variable $i (number of banners) is greater than or equal to 0, keep counting down to 0 ($i--).
I'm not sure how it works in PHP, but as far as C/C++ and Java go, this is also an interesting point to consider.
i++ and ++i might give you different results. let's say you do this:
int i = 5;
int x = i++;
Then x is set to 5 before i is incremented. But if you do:
int i = 5;
int x = ++i;
Then x is set to 6, because i is incremented first.
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
At 7/25/01 09:24 AM, MassiveBit wrote: I'm not sure how it works in PHP, but as far as C/C++ and Java go, this is also an interesting point to consider.
Exactly the same.
$i = 0;
$j = $i++;
echo "i = $i and j = $j";
// Output: i = 0 and j = 1
$i = 0;
$j = ++$i;
echo "i = $i and j = $j";
// Output: i = 1 and j = 1
Post and pre increment. I Should have mentioned that :)
- Cheesemold
-
Cheesemold
- Member since: Mar. 16, 2001
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
ooooookkkkaayyyy. Thanks for all the replys. Something still bothers me. if you were to have a link go somewhere like, www.cheese.com/process.cgi?what=swiss, would that make the value of the varible "what" be "swiss"? and can I have something in the perl script so it would go to a certain area of the script and do certain things like "goto=$what" then it would go down to whatever the varible said, so there would be crap like cheddar, showing info on cheddar, then Bleu, showing stuff for bleu cheese, and the swiss, showing crap for swiss cheese? how would I do that? not for the soul porpose of showing the statistics of cheese, it could be something else, like user information. I have a bunch of other questions too(how could users sign up, sign in...etc.) but that would take me to long to type.
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
At 7/25/01 12:35 PM, cheesemold wrote: ooooookkkkaayyyy. Thanks for all the replys. Something still bothers me. if you were to have a link go somewhere like, www.cheese.com/process.cgi?what=swiss, would that make the value of the varible "what" be "swiss"?
Yes.
and can I have something in the perl script so it would go to a certain area of the script and do certain things like "goto=$what" then it would go down to whatever the varible said, so there would be crap like cheddar, showing info on cheddar, then Bleu, showing stuff for bleu cheese, and the swiss, showing crap for swiss cheese?
Yes
how would I do that?
You'd have to put all of the details into an array, or pull in a (probably text based) file that another script / different part of the same script has written for you (which you'll most likely read and convert the data to arrays).
I have a bunch of other questions too(how could users sign up, sign in...etc.) but that would take me to long to type.
And it would take even longer to write the answers. Your best bet is to get yourself a decent book, and go from there.
- Pecos
-
Pecos
- Member since: Dec. 29, 1999
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
At 7/25/01 12:35 PM, cheesemold wrote: ooooookkkkaayyyy. Thanks for all the replys. Something still bothers me. if you were to have a link go somewhere like, www.cheese.com/process.cgi?what=swiss, would that make the value of the varible "what" be "swiss"? and can I have something in the perl script so it would go to a certain area of the script and do certain things like "goto=$what" then it would go down to whatever the varible said, so there would be crap like cheddar, showing info on cheddar, then Bleu, showing stuff for bleu cheese, and the swiss, showing crap for swiss cheese? how would I do that? not for the soul porpose of showing the statistics of cheese, it could be something else, like user information. I have a bunch of other questions too(how could users sign up, sign in...etc.) but that would take me to long to type.
For the love of god... do not use GOTOs!!
- LordAba
-
LordAba
- Member since: Nov. 2, 2000
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
At 7/25/01 03:08 PM, MassiveBit wrote:At 7/25/01 12:35 PM, cheesemold wrote: ooooookkkkaayyyy. Thanks for all the replys. Something still bothers me. if you were to have a link go somewhere like, www.cheese.com/process.cgi?what=swiss, would that make the value of the varible "what" be "swiss"? and can I have something in the perl script so it would go to a certain area of the script and do certain things like "goto=$what" then it would go down to whatever the varible said, so there would be crap like cheddar, showing info on cheddar, then Bleu, showing stuff for bleu cheese, and the swiss, showing crap for swiss cheese? how would I do that? not for the soul porpose of showing the statistics of cheese, it could be something else, like user information. I have a bunch of other questions too(how could users sign up, sign in...etc.) but that would take me to long to type.For the love of god... do not use GOTOs!!
Gotos rule!!!! Nothing like following normal OO rules, then putting a huge goto in the middle! MWAHAHAHAHAH!!!!
What may man within him hide, though angel on the outward side.
- Pecos
-
Pecos
- Member since: Dec. 29, 1999
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
At 7/26/01 01:14 AM, Lord_Aba wrote: Gotos rule!!!! Nothing like following normal OO rules, then putting a huge goto in the middle! MWAHAHAHAHAH!!!!
Heheh... don't confuse the poor kid! That's like.. buying a nice guitar, drum set, and speakers.. then playing one show and trashing all the equipment!
Just say "No" to GOTOs.

