<aside>
$$ \large \begin{array}{c} \text{A loop gives you a sequence of iterations.}\\[6pt] \text{You can use the current iteration to generate or modify data.} \end{array} $$
</aside>
<aside>
$$ \Large{\text{\Large{\text{A for loop is useful when the number of iterations matters more than the stopping condition.}}}} $$
</aside>
A loop is repetition controlled by a condition.
Loops are learned by solving problems that require repetition.
Understand loops as a way of thinking first. Then learn the syntax.
The interesting question is:
<aside>
$$ \Large{\text{What do I want to do once for each iteration?}} $$
</aside>
That's where the actual programming skill develops.
Instead of:
printf("Hello");
printf("Hello");
printf("Hello");
printf("Hello");
printf("Hello");
You can say "Do this 5 times."
for (int i = 0; i < 5; i++)
{
printf("Hello");
}
The best way to learn loops in SOPs and VEX is to progressively connect the iteration to something useful.
for (int i = 0; i < 10; i++)
iset(i, 0, 0)
i is the counter that tells the loop which iteration it's currently on.
i in calculationsfloat x = i * 0.5;
Now the iteration can drive a calculation.
addpoint(0, set(x, 0, 0));
WHAT REPEATS?
HOW MANY TIMES?
WHAT CHANGES?
WHAT DO I DO WITH THE CHANGING VALUE?
// Get the number of points we want to create
int num = chi("num");
// Start at 0, stop before "num", and increase by 1 each iteration
for (int i = 0; i < num; i++)
{
// Create a point on input 0
// Its X position is controlled by the loop counter "i"
int pt = addpoint(0, set(i, 0, 0));
}
Imagine:
num = 4
First iteration
num = 0
Create point at
(0, 0, 0)
Second Iteration
num = 0
Create point at
(1, 0, 0)
An so on
"Take the number from the
numparameter. Starting at zero, count up to that number, and for every count create a point whose X position is the current count."
for (int i = 0; i < 4; i++)
│ │ │
│ │ └─ increase i
│ └──────── keep going while true
└─────────────── start at 0
The three parts are:
for (int i = 0; i < 4; i++)
└──────┬──────┘
the rules
int i = 0
Create a variable called i and start it at 0.
i < 4
"Is
istill less than 4?"
If yes, execute the code inside { }.
If no, stop the loop.
i++
After each iteration, increase i by 1.
$$ \large i \text{ is the counter that tells the loop which iteration it's currently on.} \\ \large \text{You can use that counter to drive positions, attributes, geometry creation, etc.} $$
// Loop through every point in the geometry
for (int i = 0; i < npoints(0); i++)
{
// Generate a random color using the point number and a seed
vector col = rand(i + chf("seed"));
// Set the point's Cd (color) attribute to the generated color
setpointattrib(0, "Cd", i, col);
}
Loop through existing data → use the current iteration to identify an element → modify that element.
Because the loop runs from 0 to npoints(0) - 1, i corresponds to the point number.
// Get the number of points to create
int num = chi("num");
// Get the angle and height increments
float angle_ratio = chf("angle_ratio");
float height_ratio = chf("height_ratio");
// Create one point for each iteration
for (int i = 0; i < num; i++)
{
// Calculate the X position using cosine
float x = cos(i * $PI * angle_ratio) * 1;
// Calculate the Z position using sine
float z = sin(i * $PI * angle_ratio) * 1;
// Build the point position
// X and Z create the circular motion
// Y increases with each iteration, creating height
vector pos = set(x, i * height_ratio, z);
// Add the point to the geometry
int pt = addpoint(0, pos);
}
<aside>
</aside>