...

View Full Version : For Loop Start/End Syntax inside an if Statement



mathceleb
07-01-2010, 06:07 PM
I have a loop that needs to execute twice. The meat of the code acts the same inside both loops, so I wanted to save code and just make a dynamic start syntax. The following two blocks both bomb. When I do a regular loop with either the first or second it works.


if ($matmaster==1)
{
for ($tcol=1;$tcol<=$rowmatrix1;$tcol++)
}
else
{
for ($tcol=$tcolstart;$tcol>=1;$tcol--)
}
{


if ($matmaster==1)
{
for ($tcol=1;$tcol<=$rowmatrix1;$tcol++) {
}
else
{
for ($tcol=$tcolstart;$tcol>=1;$tcol--) {
}


Is there a way to do the above, or, is there a way to make the for statement a string to be executed as code?

stilliard
07-01-2010, 06:13 PM
You have syntax errors in both, try the following...
if ($matmaster==1)
{
for ($tcol=1;$tcol<=$rowmatrix1;$tcol++) {
}
}
else
{
for ($tcol=$tcolstart;$tcol>=1;$tcol--) {
}
}

Beagle
07-01-2010, 06:14 PM
These both bomb because your {} are unmatched.


if () {
for() {
}
} else {
for () {
}
}


that's what you need. You're missing braces.

Fou-Lu
07-01-2010, 06:15 PM
Loops cannot be logically intersected by any type of control. This applies in every language I've used.
Use your controls to declare new variables to be used within the loop condition.

mathceleb
07-01-2010, 06:56 PM
I only wanted the loop to start in the if statement, not end.

The end is later on down the road. From the post above, it sounds like it cannot be done, thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum