Skip to content

Commit 15a02a0

Browse files
committed
add listing tags
1 parent 296cd38 commit 15a02a0

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

source/ch5_loopsanditeration.ptx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
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.
1515
For example:
1616
</p>
17-
18-
<program xml:id="python-definite-loop" interactive="activecode" language="python">
17+
<listing xml:id="python-definite-loop">
18+
<program interactive="activecode" language="python">
1919
<code>
2020
for i in range(10):
2121
print(i)
2222
</code>
2323
</program>
24+
</listing>
2425

2526
<p>
2627
In Java, we would write this as:
2728
</p>
2829

29-
<program xml:id="pjava-definite-loop" interactive="activecode" language="java">
30+
<listing xml:id="java-definite-loop">
31+
<program interactive="activecode" language="java">
3032
<code>
3133
public class DefiniteLoopExample {
3234
public static void main(String[] args) {
@@ -37,6 +39,7 @@ public class DefiniteLoopExample {
3739
}
3840
</code>
3941
</program>
42+
</listing>
4043

4144
<p>
4245
Recall that the <c>range</c> function provides you with a wide variety of options for controlling the value of the loop variable.
@@ -65,18 +68,20 @@ public class DefiniteLoopExample {
6568
If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as:
6669
</p>
6770

68-
<program xml:id="python-deinite-decrement" interactive="activecode" language="python">
71+
<listing xml:id="python-deinite-decrement">
72+
<program interactive="activecode" language="python">
6973
<code>
7074
for i in range(100, -1, -5):
7175
print(i)
7276
</code>
7377
</program>
78+
</listing>
7479

7580
<p>
7681
In Java, we would write this as:
7782
</p>
78-
79-
<program xml:id="java-definite-decrement" interactive="activecode" language="java">
83+
<listing xml:id="java-definite-decrement">
84+
<program interactive="activecode" language="java">
8085
<code>
8186
public class DefiniteLoopBackward {
8287
public static void main(String[] args) {
@@ -87,7 +92,7 @@ public class DefiniteLoopBackward {
8792
}
8893
</code>
8994
</program>
90-
95+
</listing>
9196
<p>
9297
In Python, the <c>for</c> loop can also iterate over any sequence such as a list, a string, or a tuple.
9398
Java also provides a variation of its <c>for</c> loop that provides the same functionality in its so-called for each loop.
@@ -96,20 +101,21 @@ public class DefiniteLoopBackward {
96101
<p>
97102
In Python, we can iterate over a list as follows:
98103
</p>
99-
100-
<program xml:id="python-list-iteration" interactive="activecode" language="python">
104+
<listing xml:id="python-list-iteration">
105+
<program interactive="activecode" language="python">
101106
<code>
102107
l = [1, 1, 2, 3, 5, 8, 13, 21]
103108
for fib in l:
104109
print(fib)
105110
</code>
106111
</program>
112+
</listing>
107113

108114
<p>
109115
In Java we can iterate over an <c>ArrayList</c> of integers too. Note that this requires importing the <c>ArrayList</c> class.
110116
</p>
111-
112-
<program xml:id="java-list-iteration" interactive="activecode" language="java">
117+
<listing xml:id="java-list-iteration">
118+
<program interactive="activecode" language="java">
113119
<code>
114120
import java.util.ArrayList;
115121

@@ -131,13 +137,15 @@ public class ForEachArrayListExample {
131137
}
132138
</code>
133139
</program>
140+
</listing>
134141

135142
<p>
136143
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.
137144
In fact, all primitive arrays can be used in a <term>for each</term> loop.
138145
</p>
139146

140-
<program xml:id="java-for-each" interactive="activecode" language="java">
147+
<listing xml:id="java-for-each">
148+
<program interactive="activecode" language="java">
141149
<code>
142150
public class ForEachArrayExample {
143151
public static void main(String[] args) {
@@ -149,12 +157,13 @@ public class ForEachArrayExample {
149157
}
150158
</code>
151159
</program>
160+
</listing>
152161

153162
<p>
154163
To iterate over the characters in a string in Java do the following:
155-
</p>
156-
157-
<program xml:id="java-string-iteration" interactive="activecode" language="java">
164+
</p>
165+
<listing xml:id="java-string-iteration">
166+
<program interactive="activecode" language="java">
158167
<code>
159168
public class StringIterationExample {
160169
public static void main(String[] args) {
@@ -166,6 +175,7 @@ public class StringIterationExample {
166175
}
167176
</code>
168177
</program>
178+
</listing>
169179

170180
</section>
171181

0 commit comments

Comments
 (0)