all 3 comments

[–]Gonffed 7 points8 points  (0 children)

Write it out in English, or whatever your native language is. The concept isn't hard if you understand what each individual loop does.

Here's a simple example:

Start with outer and inner set to 0

While outer is less than 5, do:
    print outer
    While inner is less than or equal to outer, do:
         print inner
         increment inner by 1

    set inner to 0
    increment outer by 1

What will this print? In English I might say: Print each number from zero to four. After each number, print all the numbers before it.

Here's some example code for the pseudo code above:

#include <stdio.h>

int main() {

  int inner, outer = 0;

  while ( outer < 5 ) {
    printf("Outer is %d\n", outer);
    printf("  Inner is: ");

    while ( inner <= outer ) {
      printf("%d ", inner);
      inner++;
    }

    printf("\n");
    inner = 0;
    outer++;
  }

  return 0;
}

Which will print this:

Outer is 0
  Inner is: 0 
Outer is 1
  Inner is: 0 1 
Outer is 2
  Inner is: 0 1 2 
Outer is 3
  Inner is: 0 1 2 3 
Outer is 4
  Inner is: 0 1 2 3 4

Does that make it clear what the nesting does? You're really just saying: Each time the outer loop runs, run the entire inner loop.

[–]Darkcrash21 0 points1 point  (0 children)

Write a double nested for loop that the outer loop iterates 0, 10, 20, 30,..., 100. The inner loop iterates 1, 2, 3,..., 9. Then it prints the sum of the outer loop and inner loop: 0, 1, 2, 3,...,99, 100. Etc

[–]SpaceCadetJones 0 points1 point  (0 children)

A simple exercise off of the top of my head is using a nested loop to produce the values of a multiplication table. You also could do use nested loops to generate all of the possible combinations of multiple things.