You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch5_loopsanditeration.ptx
+52-36Lines changed: 52 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -11,22 +11,24 @@
11
11
</p>
12
12
13
13
<p><idx><c>for</c> loop</idx>
14
-
A <term>definite loop</term> is a loop that is executed a specific or definite number of times. In Python, the easiest way to write a definite loop is using the <c>for</c> loop in conjunction with the <c>range</c> function.
15
-
For example:
14
+
<idx>definite loop</idx>
15
+
A <term>definite loop</term>, also known as a <term><c>for</c> loop</term>, is a loop that is executed for a specific or <em>definite</em> number of times. In Python, the easiest way to write a definite loop is using the <c>for</c> loop structure in conjunction with the <c>range</c> function. <xrefref="python-range-options"text="type-global"/> shows the syntax for the <c>range</c> function.
@@ -37,46 +39,55 @@ public class DefiniteLoopExample {
37
39
}
38
40
</code>
39
41
</program>
42
+
</listing>
40
43
41
44
<p>
42
-
Recall that the <c>range</c> function provides you with a wide variety of options for controlling the value of the loop variable.
45
+
Recall that the <c>range</c> function provides you with a wide variety of options for controlling the value of the loop variable as shown in <xrefref="python-range-options"text="type-global"/>.
43
46
</p>
44
-
45
-
<pre>
47
+
<listingxml:id="python-range-options">
48
+
<program>
49
+
<code>
46
50
range(stop)
47
51
range(start,stop)
48
52
range(start,stop,step)
49
-
</pre>
53
+
</code>
54
+
</program>
55
+
</listing>
50
56
51
57
<p>
52
-
The Java <c>for</c> loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis.
53
-
You can think of it this way:
58
+
The Java <c>for</c> loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis.
59
+
<xrefref="java-for-loop-syntax"text="type-global"/> shows how the Java <c>for</c> loop is written.
54
60
</p>
55
-
56
-
<pre>
61
+
<listingxml:id="java-for-loop-syntax">
62
+
<program>
63
+
<code>
57
64
for (start clause; stop clause; step clause) {
58
65
statement1
59
66
statement2
60
67
...
61
68
}
62
-
</pre>
63
-
69
+
</code>
70
+
</program>
71
+
</listing>
72
+
64
73
<p>
65
-
If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as:
74
+
If you want to start at 100, stop at 0 and count backward by 5, <xrefref="python-definite-decrement"text="type-global"/> shows how the Python <c>for</c> loop is written.
@@ -131,13 +143,15 @@ public class ForEachArrayListExample {
131
143
}
132
144
</code>
133
145
</program>
146
+
</listing>
134
147
135
148
<p>
136
-
This example stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list.
137
-
In fact, all primitive arrays can be used in a <term>for each</term> loop.
149
+
<xrefref="java-list-iteration"text="type-global"/> stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list.
150
+
<xrefref="java-for-each"text="type-global"/> shows how the <c>for</c> loop can be used to iterate over all elements in a primitive array in Java.
0 commit comments