Discuss and explore the difference between additive (sequential) and multiplicative (nested) loop structures to give students practice reasoning about loops.
Share Discuss and explore the difference between additive (sequential) and multiplicative (nested) loop structures to give students practice reasoning about loops. with FacebookShare Discuss and explore the difference between additive (sequential) and multiplicative (nested) loop structures to give students practice reasoning about loops. with Twitter
- Look out for this common misconception: Students, mistakenly, believe that nested loops are loops that run in parallel.
- Example 1: The following Java code uses an additive loop structure:
for (int p=0; p<=n; p++) {- System.out.println("Hello!");
}
for (int r=0; r<n; r++) {- System.out.println("Hello!");
}
- In this example, the first loop executes in its entirety before the second loop begins.
- Example 2: The following Java code uses a multiplicative loop structure:
for (int p=0; p<=n; p++) {- for (int r=0; r<n; r++) {
- System.out.println("Hello!");
}
}
- In this example, the inner loop must fully execute before p can be incremented.