Flipping mario can be done by changing just the outer loop, if it were going from
0 (inclusive) to number of rows
n (exclusive), you could let it go from
n-1 (inclusive) to
0 (inclusive), remember to decrement instead of increment the loop counter then.
Alternatively, you could replace any occurrence of the loop counter in the outer loop's body by
n-1 minus the loop counter, again mapping range
n-1 to range
0. I'd go with changing the outer loop instead.
As in the body of a loop there could be about anything, also any kind of loop, nesting loops, and also nesting different kinds of loops, could be done to any level. If the body of the loop does not influence the loop condition (especially does not change loop variable), then you could interpret it as the body done several times. For the duration of one iteration, the loop variable could be treated as a constant.
You asked for a nested while loop, I have one nested in for loops:
/* Pythagoras */
int a, b, c;
int n = 15;
for (a = 1; a < n; a++)
c = a;
for (b = a+1; b <= n; b++)
while (a*a + b*b > c*c)
if (a*a + b*b == c*c)
printf("%d**2+%d**2=%d**2\n", a, b, c);
My inner while loop uses several variables from outside outer loop's counter variables
b, and its own
c. In this case, I want to keep the value of
c between subsequent iterations of the second loop to save some iterations, so I initialize it before the second loop.
Also, a for loop is like a while loop where loop control is all in the head.
for (A; B; C)
is equivalent to
In the while version, you would have to find the end of the loop first to check the kind of iteration, in the for loop it's nicely gathered in the first line, although the while loop is better representing the order of execution.
do while loops differ from regular while loops in that they run at least once. You'd use them for example for input you want to repeat on bad data (featuring for-loop nested in do-while loop):
printf("Please enter a prime number: ");
prime = get_int();
is_prime = (prime > 1);
for (int divider = 2; is_prime && divider * divider <= prime; divider++)
if (prime % divider == 0)
is_prime = 0;
printf("%d is not a prime number. Try again.\n", prime);
printf("Thanks for providing beautiful prime number %d.\n", prime);
or you could do sorting with that, apply some partial sorting (like bubbling), and break the loop whenever the inner loop won't find something to sort:
sorted = 1;
for (int i = 0; i < n-1; i++)
if (arr[i] > arr[i+1])
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
sorted = 0;
A regular while loop won't work, as you know if the array is sorted only after comparing its elements.
As nested loops are nothing special, just applying the loop structure multiple times, I find them hard to explain. It's similar to functions, those can call each other, creating a similar nested execution structure, even with flexible depth ("recursion"). I hope my examples did not confuse you too much, but provided some help in understanding how nested loops could be used.