Skip to content

Commit 7a3c89c

Browse files
authored
Merge pull request #290 from habibasorour/issue_286_listing
Issue 286: 5.1 listing added
2 parents bf84da2 + fab0360 commit 7a3c89c

1 file changed

Lines changed: 52 additions & 36 deletions

File tree

source/ch5_loopsanditeration.ptx

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@
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+
<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. <xref ref="python-range-options" text="type-global"/> shows the syntax for the <c>range</c> function.
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): # range(10) is a list of integers from 0 to 9
2121
print(i)
2222
</code>
2323
</program>
24+
</listing>
2425

2526
<p>
26-
In Java, we would write this as:
27+
<xref ref="java-definite-loop" text="type-global"/> shows how the <c>for</c> loop is written in Java.
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,46 +39,55 @@ public class DefiniteLoopExample {
3739
}
3840
</code>
3941
</program>
42+
</listing>
4043

4144
<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 <xref ref="python-range-options" text="type-global"/>.
4346
</p>
44-
45-
<pre>
47+
<listing xml:id="python-range-options">
48+
<program>
49+
<code>
4650
range(stop)
4751
range(start,stop)
4852
range(start,stop,step)
49-
</pre>
53+
</code>
54+
</program>
55+
</listing>
5056

5157
<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+
<xref ref="java-for-loop-syntax" text="type-global"/> shows how the Java <c>for</c> loop is written.
5460
</p>
55-
56-
<pre>
61+
<listing xml:id="java-for-loop-syntax">
62+
<program>
63+
<code>
5764
for (start clause; stop clause; step clause) {
5865
statement1
5966
statement2
6067
...
6168
}
62-
</pre>
63-
69+
</code>
70+
</program>
71+
</listing>
72+
6473
<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, <xref ref="python-definite-decrement" text="type-global"/> shows how the Python <c>for</c> loop is written.
6675
</p>
6776

68-
<program xml:id="python-deinite-decrement" interactive="activecode" language="python">
77+
<listing xml:id="python-definite-decrement">
78+
<program interactive="activecode" language="python">
6979
<code>
7080
for i in range(100, -1, -5): # start at 100, stop at 0, decrement by 5
7181
print(i)
7282
</code>
7383
</program>
84+
</listing>
7485

7586
<p>
76-
In Java, we would write this as:
87+
<xref ref="java-definite-decrement" text="type-global"/> shows how the <c>for</c> loop is written in Java.
7788
</p>
78-
79-
<program xml:id="java-definite-decrement" interactive="activecode" language="java">
89+
<listing xml:id="java-definite-decrement">
90+
<program interactive="activecode" language="java">
8091
<code>
8192
public class DefiniteLoopBackward {
8293
public static void main(String[] args) {
@@ -87,29 +98,30 @@ public class DefiniteLoopBackward {
8798
}
8899
</code>
89100
</program>
90-
101+
</listing>
91102
<p>
92103
In Python, the <c>for</c> loop can also iterate over any sequence such as a list, a string, or a tuple.
93104
Java also provides a variation of its <c>for</c> loop that provides the same functionality in its so-called for each loop.
94105
</p>
95106

96107
<p>
97-
In Python, we can iterate over a list as follows:
108+
<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.
98109
</p>
99-
100-
<program xml:id="python-list-iteration" interactive="activecode" language="python">
110+
<listing xml:id="python-list-iteration">
111+
<program interactive="activecode" language="python">
101112
<code>
102113
l = [1, 1, 2, 3, 5, 8, 13, 21] # create a list of integers
103114
for fib in l: # iterate over the list
104115
print(fib)
105116
</code>
106117
</program>
118+
</listing>
107119

108120
<p>
109-
In Java we can iterate over an <c>ArrayList</c> of integers too. Note that this requires importing the <c>ArrayList</c> class.
121+
<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.
110122
</p>
111-
112-
<program xml:id="java-list-iteration" interactive="activecode" language="java">
123+
<listing xml:id="java-list-iteration">
124+
<program interactive="activecode" language="java">
113125
<code>
114126
import java.util.ArrayList;
115127

@@ -131,13 +143,15 @@ public class ForEachArrayListExample {
131143
}
132144
</code>
133145
</program>
146+
</listing>
134147

135148
<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+
<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.
150+
<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.
138151
</p>
139152

140-
<program xml:id="java-for-each" interactive="activecode" language="java">
153+
<listing xml:id="java-for-each">
154+
<program interactive="activecode" language="java">
141155
<code>
142156
public class ForEachArrayExample {
143157
public static void main(String[] args) {
@@ -149,12 +163,13 @@ public class ForEachArrayExample {
149163
}
150164
</code>
151165
</program>
166+
</listing>
152167

153168
<p>
154-
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">
169+
<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.
170+
</p>
171+
<listing xml:id="java-string-iteration">
172+
<program interactive="activecode" language="java">
158173
<code>
159174
public class StringIterationExample {
160175
public static void main(String[] args) {
@@ -166,6 +181,7 @@ public class StringIterationExample {
166181
}
167182
</code>
168183
</program>
184+
</listing>
169185

170186
</section>
171187

@@ -329,4 +345,4 @@ public class DoWhileExample {
329345
</exercise>
330346
</reading-questions>
331347
</section>
332-
</chapter>
348+
</chapter>

0 commit comments

Comments
 (0)