Professional Documents
Culture Documents
Topic6NestedForLoops 4up PDF
Topic6NestedForLoops 4up PDF
Topic 6
A for loop can contain any kind of statement in its body,
Nested for Loops including another for loop.
– The inner loop must have a different name for its loop counter
"Complexity has and will maintain a strong fascination for many people. It variable
i bl so th
thatt it will
ill nott conflict
fli t with
ith th
the outer
t lloop.
is true that we live in a complex world and strive to solve inherently
nested loop: Loops placed inside one another, creating a
complex problems, which often do require complex mechanisms. loop of loops.
H
However, thi
this should
h ld nott di
diminish
i i h our d
desire
i ffor elegant
l t solutions,
l ti which
hi h for (int i = 1; i <= 3; i++) {
convince by their clarity and effectiveness. Simple, elegant solutions are for (int j = 1; j <= 2; j++) {
more effective, but they are harder to find than complex ones, and they System.out.println("six");
require more time,
time which we too often believe to be unaffordable " }
-Niklaus Wirth }
Output:
six
six
six
six
Based on slides for Building Java Programs by Reges/Stepp, found at
six
http://faculty.washington.edu/stepp/book/
p y g pp
six
CS305j Introduction to Computing Nested For Loops 1 CS305j Introduction to Computing Nested For Loops 2
All
All of the statements in the outer loop's
loop s body are
Whatt iis th
Wh the output
t t off th
the ffollowing
ll i nested
t d
executed 5 times. for loop?
– The inner loop runs 10 times for each of those 5 times,
for a total of 50 numbers printed.
for (int i = 1; i <= 4; i++) {
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) {
for (int j = 1; j <= 10; j++) { S
System.out.print("*");
i ("*")
System.out.print((i * j) + " "); }
} System.out.println();
S t
System.out.println();
t i tl () // to
t endd the
th }
line
}
Output?
Output?
Output:
1 2 3 4 5 6 7 8 9 10
Do you really need a nested
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
for loop in this case?
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
CS305j Introduction to Computing Nested For Loops 3 CS305j Introduction to Computing Nested For Loops 4
Nested for loop exercise Nested for loop exercise
Output:
*
**
***
Output?
Output?
****
*****
******
CS305j Introduction to Computing Nested For Loops 5 CS305j Introduction to Computing Nested For Loops 6
CS305j Introduction to Computing Nested For Loops 7 CS305j Introduction to Computing Nested For Loops 8
Common nested loop bugs How to comment: for loops
CS305j Introduction to Computing Nested For Loops 9 CS305j Introduction to Computing Nested For Loops 10
Variable scope
scope:
scope: The portion of a program where a given
variable exists.
– A variable's scope is from its declaration to the end of the
block (pair of { } braces) in which it was declared.
• If a variable is declared in a for loop (including the
<initialization> or the loop's <statement(s)>, it exists until the
Variable Scope end of that for loop.
• If a variable is declared in a method, it exists only in that method,
until the end of the method.
CS305j Introduction to Computing Nested For Loops 13 CS305j Introduction to Computing Nested For Loops 14
Making
g the 3 a g
global constant removes the redundancy:
y
– the global constant is declared outside methods, but inside the program,
Consider the task of drawing the following figure:
public class PrintFigure{ +/\/\/\/\/\+
public static final int MAX_VALUE = 3; | |
public static void main(String[] args) { +/\/\/\/\/\+
printTop();
printBottom();
} +/\/\/\/\/\+
public static void printTop() { | |
for (int i = 1; i <= MAX_VALUE; i++) {
for (int j = 1; j <= i; j++) { | |
System.out.print(j); | |
}
System.out.println(); | |
} | |
}
+/\/\/\/\/\+
public static void printBottom() {
for (int i = MAX_VALUE; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
}
System.out.print(MAX_VALUE);
Each figure is strongly tied to the number 5 (or a
}
System.out.println();
multiple such as 10 ...)
}
}
S code
See d ffor drawing
d i ththese fi
figures.
CS305j Introduction to Computing Nested For Loops 17 CS305j Introduction to Computing Nested For Loops 18
System.out.print("|");
S t t i t("|")
for (int i = 1; i <= 2 * FIGURE_WIDTH; i++) {
System.out.print(" ");
}
System.out.println("|");
drawPlusLine();
}
CS305j Introduction to Computing Nested For Loops 21