2 ms·
Not quite sure what you meant with "inner loop consumes an array of size size greater than that of the outer loop" but I'd like to say no. In the graphic, we c
by tsumnia 10y ago
Not quite sure what you meant with "inner loop consumes an array of size size greater than that of the outer loop" but I'd like to say no.
In the graphic, we create two separate variables. The outer loop is only controlled through the X variable, meaning only when X is greater than or equal to 3 does it cease. If X started at a higher than 3 number, it doesn't run at all, but that's not the point of the example. The confusing part comes with the Y.
Y is the controller of the inner loop, and only gets incremented when the Y loop iterates. Once the inner loop completes, it still has 2 more instructions to execute (that belong to the outer loop), so it does them. That last line resets the Y variable, so that it can run the inner loop the next time.
It's actually a good question I ask as a follow-up. Suppose I delete the "y = 0;" line, how does that change how the loop behaves?
At no point does my X variable manipulate my Y variable directly (through loops yes, but not like y = x * 2). Like if I went through an old fashion phone book or dictionary, there are 26 letters (outer loop), but each letter has a varying number of words (inner loop). Then it'd read something like
while (x < 26)
{
while (y < NUMBER_OF_WORDS_STARTING_WITH_X)
{
y = y + 1;
}
x = x + 1;
y = 0; // otherwise, Y never resets
}