Notifications
Clear all

[Closed] selecting every nth loop

hey folks!
I gotta super simple question, I’m sure it’s just a matter of me not knowing the proper syntax in maxscript.

  let's say I've got a simple loop
for i = 1 to 20 do (
  and I want to do something every 2nd loop. In every other scripting language I've encountered, I could simply write an if statement like...
if i%2 then (
  ...that basically says "if [i]i[/i] is divisible by two, then do the stuff". maxscript has gotta have something like the % operator, but obviously the above code doesn't work, and I can't find any alternatives anywhere... Any ideas?
 
 thanks guys!
5 Replies

There are two ways of doing this, depending on whether you wanted to do anything at all on the other half of the loop iterations:

 for i=1 to 20 by 2 do () 
--This executes the loop code on every other loop iteration
for i=1 to 20 do (if (not ((mod i 2)>0.0)) do ())
--this executes the loop code each time, but the specific statement only on every other iteration

Hi,

MAXScript doesn’t have a % operator like C++ does.

It can be done like John suggested, however the second for loop could be written like so:

for i = 1 to 20 where mod i 2 == 0 do …

Light

I could be wrong about this, but I thought I remembered Larry Minton saying to avoid direct comparisons using floats, as they will sometimes evaluate unpredictably. In other words, (0.0==0) will sometimes evaluate as false. Its been a while, but I think I’ve experienced the problem myself. I wrote the code above to avoid the possibility of this behavior.

Does this sound familiar to anyone, or am I remembering this wrong?

cool, that’ll do the trick! As usual, maxscript needs a workaround for something every other language can manage easily…

anyway, thanks guys!