Skip to content

Commit 47f4084

Browse files
committed
add listing to text
1 parent 49cdd3f commit 47f4084

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

source/ch5_loopsanditeration.ptx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
</p>
1212

1313
<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. <xref ref="python-range-options" text="type-global"/> shows the syntax for the <c>range</c> function.
1615
</p>
1716
<listing xml:id="python-definite-loop">
1817
<program interactive="activecode" language="python">
@@ -24,7 +23,7 @@ for i in range(10):
2423
</listing>
2524

2625
<p>
27-
In Java, we would write this as:
26+
<xref ref="java-definite-loop" text="type-global"/> shows how the <c>for</c> loop is written in Java.
2827
</p>
2928

3029
<listing xml:id="java-definite-loop">
@@ -42,23 +41,24 @@ public class DefiniteLoopExample {
4241
</listing>
4342

4443
<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 <xref ref="python-range-options" text="type-global"/>.
4645
</p>
47-
48-
<program xml:id="python-range-options">
46+
<listing xml:id="python-range-options">
47+
<program>
4948
<code>
5049
range(stop)
5150
range(start,stop)
5251
range(start,stop,step)
5352
</code>
5453
</program>
54+
</listing>
5555

5656
<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+
<xref ref="java-for-loop-syntax" text="type-global"/> shows how the Java <c>for</c> loop is written.
5959
</p>
60-
61-
<program xml:id="java-for-loop-syntax">
60+
<listing xml:id="java-for-loop-syntax">
61+
<program>
6262
<code>
6363
for (start clause; stop clause; step clause) {
6464
statement1
@@ -67,12 +67,13 @@ public class DefiniteLoopExample {
6767
}
6868
</code>
6969
</program>
70-
70+
</listing>
71+
7172
<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, <xref ref="python-definite-decrement" text="type-global"/> shows how the Python <c>for</c> loop is written.
7374
</p>
7475

75-
<listing xml:id="python-deinite-decrement">
76+
<listing xml:id="python-definite-decrement">
7677
<program interactive="activecode" language="python">
7778
<code>
7879
for i in range(100, -1, -5):
@@ -82,7 +83,7 @@ for i in range(100, -1, -5):
8283
</listing>
8384

8485
<p>
85-
In Java, we would write this as:
86+
<xref ref="java-definite-decrement" text="type-global"/> shows how the <c>for</c> loop is written in Java.
8687
</p>
8788
<listing xml:id="java-definite-decrement">
8889
<program interactive="activecode" language="java">
@@ -103,7 +104,7 @@ public class DefiniteLoopBackward {
103104
</p>
104105

105106
<p>
106-
In Python, we can iterate over a list as follows:
107+
<xref ref="python-list-iteration" text="type-global"/> shows how the <c>for</c> loop can be used to iterate over a list in Python.
107108
</p>
108109
<listing xml:id="python-list-iteration">
109110
<program interactive="activecode" language="python">
@@ -116,7 +117,7 @@ for fib in l:
116117
</listing>
117118

118119
<p>
119-
In Java we can iterate over an <c>ArrayList</c> of integers too. Note that this requires importing the <c>ArrayList</c> class.
120+
<xref ref="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.
120121
</p>
121122
<listing xml:id="java-list-iteration">
122123
<program interactive="activecode" language="java">
@@ -144,8 +145,8 @@ public class ForEachArrayListExample {
144145
</listing>
145146

146147
<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+
<xref ref="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+
<xref ref="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.
149150
</p>
150151

151152
<listing xml:id="java-for-each">
@@ -164,7 +165,7 @@ public class ForEachArrayExample {
164165
</listing>
165166

166167
<p>
167-
To iterate over the characters in a string in Java do the following:
168+
<xref ref="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.
168169
</p>
169170
<listing xml:id="java-string-iteration">
170171
<program interactive="activecode" language="java">

0 commit comments

Comments
 (0)