for(int i = 0; i < points; i++) explanation?

   178   4   2
User Avatar
Member
318 posts
Joined: April 2017
Offline
Hi!

I'm learning Vex and I've often seen this type of coding to iterate multiple times. I was hoping someone could explain each sections to me.

int i = 0; (is “i” a temporary variable. why is it equal to 0?)
i < points; (I'm guessing the value “points” tells it when to stop the iterations)
i++ (??)

On side fx, I can find info on “foreach” but nothing covers “for”…

Thanks for any help!

-Olivier
User Avatar
Member
1292 posts
Joined: Sept. 2015
Offline
int i = 0; (is “i” a temporary variable. why is it equal to 0?)

Yes it is ‘temporary’ in the sense it's only seen by the wrangle it was written in.

And it being equal to zero is an arbitrary value, but tends to prove most useful.

Sometimes for example you may want to access a memember of an array that corresponds to the current iteration number, and since arrays are indexed starting at zero, setting your first index to zero makes a one to one correspondance to your arrays.

But again this is arbitrary and you don't have to go with such a convention, actually for more complex looping set-ups sometimes starting at 1 can be better, or even at a Max number if you need to ‘count down’ instead of up.

It all depends on your needs.

i < points; (I'm guessing the value “points” tells it when to stop the iterations)

This is a conditional statement that basically says to keep going through the loop so long as the value of i is less than the value of points. When i is equal or more, the loop will not execute.

i++ (??)

At the end of a loop cycle the value of i will be increment by 1;

It's a short form and convenient way to write the following which would give the same results;

i = i + 1 or i += 1

This is tied to your previous asking of what i < points is;

Again at the end of the current loop cycle when everything has been processed within the curly braces, i in this case is incremented by 1;

So now the comparison of whether i is still less than points determines whether another cycle of what's within the curly braces is executed or not.

And the same as starting with int = 0 is arbitrary, so is how many times i is incremented per loop cycle. You may want to increment the i value by 10 for each cycle for example, with i += 10. It can be very usefull to do this as a type of filtering. Again it depends on your purpose.

The info on ‘for’ is in the docs although it's description is brief; But that's only because in this case it follows the same syntax structure as it's use in C and C++. It's very common and used much in programming that is not unique to Houdini.

You would be able to net search many examples in the use of for loops and even if the content could not be applied within Houdini, the example of its' logical flow would be able to be applied to Houdini.

http://www.sidefx.com/docs/houdini/vex/statement.html [www.sidefx.com]
User Avatar
Member
159 posts
Joined: Nov. 2010
Offline
One-stop VEX shopping:

http://www.tokeru.com/cgwiki/index.php?title=HoudiniVex [www.tokeru.com]

also home of the scintillating (the title at least) Joy of VEX:

http://www.tokeru.com/cgwiki/index.php?title=JoyOfVex [www.tokeru.com]

cheers, k
User Avatar
Member
466 posts
Joined: Oct. 2016
Offline
Moin,

BabaJ's explanation is complete, but maybe a different wording helps, too …

“for” is one way of creating a loop. A loop is a block of program code that gets executed over and over again - usually, with start conditions being set, end conditions being checked and something being done after each iteration of the loop has ended.
In the case of “for”, these three components of a loop are defined as:

for ( Setup ; End-Condition-Check ; What-To-Do-After-Each-Run )

Setup: You do not NEED to set up anything, if you can check for something to have happened inside the loop. However, in most cases you want a loop to run a defined number of times, so you need a “timer”. The variable “i” is that timer. A timer has to start somewhere - you can either set it to your maximum number of runs/iterations (which would be the number stored in the variable “points”) here and then subtract 1 from your timer after each run of the loop OR you start at a given value (0 or 1, usually) and increase the timer after each iteration.
Saying “i=0” sets the timer to 0. It's 0, because in the computer world things start at 0, not at 1.
That's all the for-loop sets up in your case. You get a local (temporary) variable (your timer i) and after each run that timer is increased by 1 (the short form “i++” being lazy-programmers-make-it-look-like-magic speak) and compared against your end-definition.

End-Condition-Check: Before the iteration is run, this condition is being checked. Saying “i<points” will be a TRUE statement, as long as your timer “i” is smaller than the number in “points”, and FALSE once it is equal (or larger) than points. The loop will stop once the condition-check returns FALSE. As long as it returns TRUE, the loop will continue.
You could have a statement like “1==1” in that second part of the loop-definition. That comparison would always be true (1 always equals 1). So the loop would run indefinitely. You could BREAK it from inside the loop once some other condition is met.
You could have a statement like “1==0” in that second part of the loop-definition. That comparison would always be false (1 is never equal to 0), so the loop would NOT RUN, because BEFORE the iteration starts, this condition is checked and, being false, the loop would not be allowed to do anything.

What-To-Do-After-Each-Run: Once an iteration is done, the program code in this block is executed and the next iteration is started (if the end-condition-check returns true). Saying “i++” is the same as saying “i=i+1”. But since programmers are lazy AND want non-programmers to think that they “know things” and “can do magic”, they came up with a lot of crazy ways of typing things that no sane person would understand.
It's like learning how to understand women. It does not have to make sense, it's about accepting the facts of life and that some things are just … weird.
I digress.

I hope this, even though it is basically the same that BabaJ said, is of some help. If you like this style of explain-o-matic, check out my book about “how to become a programmer” [www.sidefx.com]. It's not about VEX, but does say a lot of things about loops, variables, women and life.

Marc

Software Sewers Service (aka Pipeline Problems Solver)
https://vimeo.com/marcalbrecht [vimeo.com] - https://www.marc-albrecht.de [www.marc-albrecht.de]
User Avatar
Member
318 posts
Joined: April 2017
Offline
Super!

Thanks for all the info! This helps a lot and feels less alienating than before.

-Olivier
  • Quick Links