Professional Documents
Culture Documents
Topic 6
Nested for Loops
"Complexity has and will maintain a strong fascination for many people. It
is true that we live in a complex world and strive to solve inherently
complex problems, which often do require complex mechanisms.
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
convince by their clarity and effectiveness. Simple, elegant solutions are
more effective, but they are harder to find than complex ones, and they
require more time,
time which we too often believe to be unaffordable "
-Niklaus Wirth
Whatt iis th
Wh
the output
t t off th
the ffollowing
ll i nested
t d
for loop?
Output?
Output?
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
Nested For Loops
All
All of the statements in the outer loop's
loop s body are
executed 5 times.
Output:
six
six
six
six
six
six
The inner loop must have a different name for its loop counter
variable
i bl so th
thatt it will
ill nott conflict
fli t with
ith th
the outer
t lloop.
Whatt iis th
Wh
the output
t t off th
the ffollowing
ll i nested
t d
for loop?
Output:
*
**
***
****
*****
******
Output?
Output?
A for loop can have more than one loop nested in itit.
....1
...22
..333
.4444
55555
Answer:
1
22
333
4444
55555
Bad:
// This loop repeats 10 times
times, with i from 1 to 10
10.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) { // loop goes 5 times
System.out.print(j); // print the j
}
System.out.println();
}
Better:
// Prints 12345 ten times on ten separate lines.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(j);
}
System.out.println(); // end the line of output
}
Wh
Whatt iis th
the output
t t off th
the ffollowing
ll i nested
t d lloops?
?
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; i++) {
System out print(j);
System.out.print(j);
}
System.out.println();
}
CS305j Introduction to Computing
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
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.
Variable Scope
11
12
Overlapping scope
13
14
Global constants
15
16
Making
g the 3 a g
global constant removes the redundancy:
y
the global constant is declared outside methods, but inside the program,
+/\/\/\/\/\+
|
|
+/\/\/\/\/\+
+/\/\/\/\/\+
|
|
|
|
|
|
|
|
|
|
+/\/\/\/\/\+
S code
See
d ffor drawing
d
i th
these fi
figures.
}
CS305j Introduction to Computing
17
Output:
+/\/\/\/\/\+
|
|
+/\/\/\/\/\+
18
A
A normal variable cannot be used to fix the
problem, because it is out of scope:
19
// ERROR
20
21