[Closed] do..while and continue
Well i don’t know, this may be a feature in maxscript, but surely it’s quite strange to me
This mxs code evaluates to an endless loop
(
local i = 1
local b = true
do (
print i
if(i==5) do (
b = false
continue
)
i += 1
)
while (b)
)
while this c++ code prints the numbers up to 5
#include "iostream"
int main()
{
int i = 1;
bool b = true;
do{
std::cout<<i<<std::endl;
if(i==5){
b = false;
continue;
}
i++;
}
while(b);
}
am I missing something
Get rid of the “continue” and it works. Not sure why you need it here? Been trying to find a better answer/workable ‘continue’ but after putting max into endless loops a dozen times, I’ll let someone who already knows the real answer try…
I need to check the help file, but I think that the exit command should replace the continue – this gets you out of the loop at the right point.
Alex
CONTINUE is used in MAXScript to SKIP all following lines to the end of the loop’s body and repeat the loop again. It looks like this also skips the loop’s WHILE test and just goes straight to the beginning. Since this also skips the increment of I which is AFTER the CONTINUE, once I becomes == 5 it never increments again and the CONTINUE jumps to the beginning forever!
CONTINUE has no place in this code, in fact using a DO WHILE loop like this is a very bad idea – if you want to loop from 1 to 5, there are much better and faster ways.
I am pretty sure I put a benchmark in the Reference showing how the manual management of the I variable inside the loop eats time compared to a FOR loop.
Also, using EXIT is even worse as it is very slow – using WHILE in a FOR loop is the way to go. There was a Mandelbrot example last week on this forum where getting rid of the EXIT call in the loop caused a 200% speedup!
@Professor420
well in this simple example, really using “continue” is pointless. In the real code I was trying to write, there is quite a lot of code after the “if”
@Alex Morris
yes “exit” does pretty good job in my case, in fact I ended using it, but I was trying to avoid “exit” because I read in the reference that it’s very slow
@Bobo
well my actual goal wasn’t to loop from 1 to 5 =) I just want my loop to perform at least once and then terminate if a certain condition is met, but I don’t want to use “exit”. I’ll try the while in the for loop.
thanx to all
@Professor420
well in this simple example, really using “continue” is pointless. In the real code I was trying to write, there is quite a lot of code after the “if”
@Alex Morris
yes "exit" does pretty good job in my case, in fact I ended using it, but I was trying to avoid "exit" because I read in the reference that it's very slow
@Bobo
well my actual goal wasn't to loop from 1 to 5 =) I just want my loop to perform at least once and then terminate if a certain condition is met, but I don't want to use "exit" I'll try the while in the for loop.
It looks like this also skips the loop's WHILE test and just goes straight to the beginning
maybe a bug ?
thanx to all