Notifications
Clear all

[Closed] 3d Max Maxscript Essentials Book question

This code produces 7 teapots stacked in x and y
I think I understand it all except why the last (7th) Teapot
has a position in x of 120 yet the boolean while statement
says t.pos.x < 101 why is there a teapot at 120?
Shouldn’t the loop stop at 100 as it is looping every at every
20 units and is under 101?


i = 0
t
do
(
 t = teapot pos:[i*20, 0, i*20]
 i = i+1
) while t.pos.x < 101

If I edit the code to

while t.pos.x < 100

or even 80 it stops at the correct value

Appreciate any help with this

4 Replies

It happens, because the while is after the execution of the loop. So a teapot will be create at 120, and after creation it will stop, because that last teapot.pos.x was greater than 101. Basically what the while is doing is the following:
0 < 101, yes create another
20 < 101, yes create another
40 < 101, yes create another
60 < 101, yes create another
80 < 101, yes create another
100 < 101, yes create another
120 < 101, no it’s greater don’t create any more

-Eric

ok thanks Eric that makes sense

 j83

A do-while loop is often used where you want something to run at least once. For example, were you writing a script or program where you wanted something to repeat until the user wanted to quit, you’d do something like,


  (
  	do
  	(
  		messageBox "Hello my friend." beep:false
  	)
  	while (querybox("Continue?"))
  	print "Done."
  )
  

However, for-loops are much more preferred when you do not need such functionality.

Thanks Jonathan very helpful.

My brain got stuck trying to understand this section in the book and I couldn’t resolve it. The simple answer from Eric got me back on track and your example gave it context.

The book seems ok but seems to be more of an intermediate level book that revisits some of the basics but without lengthy explanations.

Not sure if it’s the medication or the late nights or if I’m not suited to programming but… :banghead: it will be made to enter the brain one way or another.