PDA

View Full Version : break; Command


dunkel3d
08-29-2003, 05:31 AM
I hope this makes sence. I'm using the break; command in a for loop within 2 parent for loops.
It seems to terminate all 3 loops. Correct? Is there a way to terminate the last child loop only?

S

ajk48n
08-29-2003, 09:47 AM
Not quite sure at the moment, but try "continue;"

misterdi
08-29-2003, 01:31 PM
It doesn't break the two parent loop in the test I made.

Here is the test code

for ($i = 1; $i<10; $i++)
{
for ($j = 1; $j<10; $j++)
{
for ($k = 1; $k<10; $k++)
{
if ($k==5)
{
print ($i + " " + $j + " " + $k + "\n");
break;
}
}
}
}


And here is the printout:


1 1 5
1 2 5
1 3 5
.....
9 5 5
9 6 5
9 7 5
9 8 5
9 9 5


It only break the lowest loop.

Best regards,

misterdi
08-29-2003, 01:38 PM
Sorry the previous code doesn't really show break; command, so I redo the test like this:


for ($i = 1; $i<3; $i++)
{
for ($j = 1; $j<3; $j++)
{
for ($k = 1; $k<10; $k++)
{
print ($i + " " + $j + " " + $k + "\n");
if ($k==5)
{
break;
}
}
}
}


The output is :


1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 2 1
1 2 2
1 2 3
........
2 1 5
2 2 1
2 2 2
2 2 3
2 2 4
2 2 5

CGTalk Moderation
01-16-2006, 12:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.