Nested For Loops
Introduction: 4.4.0
- A
nested loop
has one loop inside of another- Used for working with two dimensions
- rows and columns
- Used for working with two dimensions
- When a loop is nested, it runs many times inside an outer loop
- Every time the outer loop iterates, the inner loop runs
// make a triangle!
for (int depth = 1; depth <= 5; depth++) {
for (int i = 0; i < depth; i++) {
System.out.print("*");
}
System.out.println(); // Print a new line!
}
// output
//
// *
// **
// ***
// ****
// *****
Summary: 4.4.3
- Nested iteration statements are iteration statements that appear in the body of another iteration statement
- The inner loop must complete all it’s iterations before the outer loop iterates again
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.