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
+20-19Lines changed: 20 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,7 @@
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
+
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. <xrefref="python-range-options"text="type-global"/> shows the syntax for the <c>range</c> function.
<xrefref="java-definite-loop"text="type-global"/> shows how the <c>for</c> loop is written in Java.
28
27
</p>
29
28
30
29
<listingxml:id="java-definite-loop">
@@ -42,23 +41,24 @@ public class DefiniteLoopExample {
42
41
</listing>
43
42
44
43
<p>
45
-
Recall that the <c>range</c> function provides you with a wide variety of options for controlling the value of the loop variable.
44
+
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"/>.
46
45
</p>
47
-
48
-
<programxml:id="python-range-options">
46
+
<listingxml:id="python-range-options">
47
+
<program>
49
48
<code>
50
49
range(stop)
51
50
range(start,stop)
52
51
range(start,stop,step)
53
52
</code>
54
53
</program>
54
+
</listing>
55
55
56
56
<p>
57
-
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.
58
-
You can think of it this way:
57
+
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.
58
+
<xrefref="java-for-loop-syntax"text="type-global"/> shows how the Java <c>for</c> loop is written.
59
59
</p>
60
-
61
-
<programxml:id="java-for-loop-syntax">
60
+
<listingxml:id="java-for-loop-syntax">
61
+
<program>
62
62
<code>
63
63
for (start clause; stop clause; step clause) {
64
64
statement1
@@ -67,12 +67,13 @@ public class DefiniteLoopExample {
67
67
}
68
68
</code>
69
69
</program>
70
-
70
+
</listing>
71
+
71
72
<p>
72
-
If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as:
73
+
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.
In Java we can iterate over an <c>ArrayList</c> of integers too. Note that this requires importing the <c>ArrayList</c> class.
120
+
<xrefref="java-list-iteration"text="type-global"/> shows how the <c>for</c> loop can be used to iterate over an <c>ArrayList</c> of integers in Java.
120
121
</p>
121
122
<listingxml:id="java-list-iteration">
122
123
<programinteractive="activecode"language="java">
@@ -144,8 +145,8 @@ public class ForEachArrayListExample {
144
145
</listing>
145
146
146
147
<p>
147
-
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.
148
-
In fact, all primitive arrays can be used in a <term>for each</term> loop.
148
+
<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.
149
+
<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.
149
150
</p>
150
151
151
152
<listingxml:id="java-for-each">
@@ -164,7 +165,7 @@ public class ForEachArrayExample {
164
165
</listing>
165
166
166
167
<p>
167
-
To iterate over the characters in a string in Java do the following:
168
+
<xrefref="java-for-each"text="type-global"/> shows how the <c>for</c> loop can be used to iterate over all elements in a string in Java.
0 commit comments