Jump to content

the difference between foreach and while


Koyotemohn

Recommended Posts

Hello I just got my Indie license and I had been using the apprentice HD for a while.

 

Anyhow I have been studying Vex code and I know it is probably a noob question but I just wanted to understand something.

 

the for each loop function is for arbitrary iterations 

 

and the while loop is for point clouds?

 

To me it seems like they do the same or similar thing.

 

I am just trying to properly wrap my head around it.

 

thanks in advance.

Link to comment
Share on other sites

they are both loops, so yeah they kind of do the same thing.

 

while basically loops as long as the expression in the () is true.  in terms of pointclouds, there is "pciterate()" which returns true as long as there are more points to process in the pc you specify and it also advances the point counter by 1 so your subsequent pc functions are pointing the right point.

 

but you can also use while in other instances where you just want to loop until you've satisfied some condition.  like run x number of times or keep trying until you get below a certain error threshold or whatever.... it's pretty generic.

 

foreach also loops, but it's made for operating on arrays.  it works with the newer point cloud functions which return arrays (as opposed to the old ones that return handles).  since foreach requires a pre-constructed array of every value you want to run your loop against, it's not as generally useful as a while loop.

Link to comment
Share on other sites

you can use for loop with pointcloud funcion returning handle as well, thanks to pcnumfound() and pcimportbyindex?() functions, which is especially much cleaner in VOPs than while loop

but for functionality it ultimately doesn't matter as you can always break our of for loop sooner than the max iteration numbers are reached or count iterations in while loop

you can as well use do loop if you want to execute at least once before evaluating condition

and foreach as mentioned useful for arrays

Link to comment
Share on other sites

Tomas thank you for the additional information. 

 

So you are saying that using the for loop with in the VOPS(versus point wrangle/attribute wrangle VEX code) is cleaner to assemble in VOPS than implementing the same operation in VOPS using the while loop?

 

I understood how to use VOPS in a rather linear sense...but I hit a wall when I used the for loop and other iterative visually.

 

I get how to use the Solver Sop, which is amazing with the switch sop.

 

So I started studying how to use wrangle nodes in hopes to somehow compartmentalize the process.

 

anyhow thanks again for taking the time out of your schedule for the insight.

Link to comment
Share on other sites

what I meant is that iterating over data from Pcopen VOP is much cleaner and simpler using For Loop VOP than While Loop VOP

but obviously for other cases the choice usually is based on whether you know the number of iterations beforehand (For) or not (While)

Link to comment
Share on other sites

loops in vops suck in general.  i think it was pcloud manipulation that really pushed me towards writing inline code everywhere.  now i find it's easier to just code than use vops for most things.

yup, since wrangles I barely touch VOPs, maybe still for assets with heavy use of various noises as that's still easier in VOPs than to write it all in VEX

Link to comment
Share on other sites

like tomas, i use wrangles/snippets for pretty much everything and will use vops for convenience nodes like noise or maybe transforms (since i can never remember the parm order -- to, from?  from, to?)

 

i even tend to not bother with plugging in parms when using inline code and instead just call the parm directly by name.  this can get me into trouble because i also tend to leave off the $ when defining my own variables and so if i end up with two inline nodes, i might bump into name collisions.

 

i don't tend to do anything that requires external code.  most of the time, it's just simple math or looping.  if i'm doing something esoteric that might need library support, i'd probably just push out my results and feed that to whatever is needed.  i don't tend to chain multiple inline's, but i do chain wrangles since it's kind of a different animal.  but with wrangle it's more about having logical steps more than a setup stage and an execution stage.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...