1

I watched the video below and am curious how to I do nested for loops that contain more than 2 for loops ?

I don't understand how to reverse the pyramid does anyone know? By reverse I mean 5 hashes on the top and 1 on the bottom.

What is a nested while loop and a nested do while loop?

And i generally need a better video on nested for loops in c.

Any suggestions? Please do not post a video that is considered cheating on the Mario assignment.

Thanks

https://www.youtube.com/watch?v=76UAxsNiF14

1

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 0 to n-1 to range n-1 to 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)
        {
            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 a and 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)
    D;

is equivalent to

{
    A;
    while (B)
    {
        D;
        C;
    }
}

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):

int prime;
int is_prime;
do
{
    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;
        }
    }
    if (!is_prime)
    {
        printf("%d is not a prime number. Try again.\n", prime);
    }
}
while (!is_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:

int sorted;
do
{
    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;
        }
    }
}
while (!sorted);

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.

5
  • How do I add code to stack exchange in comments I have tried ` but nothing happens. I have a few questions. – Tom55555 Jan 30 '17 at 2:10
  • In comments, you can do inline code (no line breaks) with one of those backticks ` on each side. `code` => code – Blauelf Jan 30 '17 at 7:28
  • ` // calculate how much water used in a shower { int minutes ; do { printf ("pick a number of bottles minute\n ") ; minutes = GetInt() ; // getint = a number } while ( minutes <= 0 ); // only print false condition and only postive numbers int bottles = 1 * 12 ; { printf ("minutes: %d " , minutes); printf ("bottles: %d " , bottles); } }` => Why does "while ( minutes <= 0 );" only execute when false, but sometimes executes when true? Keep it simple please. – Tom55555 Jan 31 '17 at 4:57
  • I tried and my code appears like above. My main question if you can't read is "while ( minutes <= 0 )" prints positive number but some instead while loops can execute while positive. It is a do while loop to be exact. Can you explain ? – Tom55555 Jan 31 '17 at 5:00
  • I can read. But your question is confusing. The do..while loop itself is a control structure, the body executes at least once (the only difference to a regular while loop), and keeps doing so as long as after each iteration, the loop condition is true (or at least not 0, which is the only equivalent of false, while everything else is considered true in that case). – Blauelf Jan 31 '17 at 9:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .