Skip to content

Commit fec9de2

Browse files
committed
Merge branch 'master' into issue_289_comment
2 parents ab399a1 + 7528344 commit fec9de2

2 files changed

Lines changed: 65 additions & 47 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -729,36 +729,54 @@ public class Histo {
729729
</p>
730730

731731
<p>
732-
The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact <c>for (Integer i = 0; i &lt; 10; i++)</c> is exactly equivalent to the Python <c>for i in range(10)</c> The first statement inside the parenthesis declares and initializes a loop variable <c>i</c>. The second statement is a Boolean expression that is our exit condition. In other words we will keep looping as long as this expression evaluates to true. The third clause is used to increment the value of the loop variable at the end of iteration through the loop. In fact <c>i++</c> is Java shorthand for <c>i = i + 1</c> Java also supports the shorthand <c>i--</c> to decrement the value of i. Like Python, you can also write <c>i += 2</c> as shorthand for <c>i = i + 2</c> Try to rewrite the following Python for loops as Java for loops:
733-
</p>
734-
735-
<p>
736-
<ul>
737-
<li>
738-
<p>
739-
<c>for i in range(2,101,2)</c>
740-
</p>
741-
</li>
732+
The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact <c>for (Integer i = 0; i &lt; 10; i++)</c> is exactly equivalent to the Python <c>for i in range(10)</c> The first statement inside the parenthesis declares and initializes a loop variable <c>i</c>. The second statement is a Boolean expression that is our exit condition. In other words we will keep looping as long as this expression evaluates to true. The third clause is used to increment the value of the loop variable at the end of iteration through the loop. In fact <c>i++</c> is Java shorthand for <c>i = i + 1</c> Java also supports the shorthand <c>i--</c> to decrement the value of i. Like Python, you can also write <c>i += 2</c> as shorthand for <c>i = i + 2</c>. </p>
742733

743-
<li>
744-
<p>
745-
<c>for i in range(1,100)</c>
746-
</p>
747-
</li>
748-
749-
<li>
750-
<p>
751-
<c>for i in range(100,0,-1)</c>
752-
</p>
753-
</li>
754-
755-
<li>
756-
<p>
757-
<c>for x,y in zip(range(10),range(0,20,2))</c> [hint, you can separate statements in the same clause with a ,]
758-
</p>
759-
</li>
760-
</ul>
761-
</p>
734+
<exercise label="matching-python-java-loops">
735+
<statement>
736+
<p>
737+
Match each Python <c>for</c> loop with its equivalent Java <c>for</c> loop.
738+
</p>
739+
</statement>
740+
741+
<matches>
742+
743+
<match>
744+
<premise><c>for i in range(2, 102, 2)</c></premise>
745+
<response><c>for (int i = 2; i &lt; 102; i += 2)</c></response>
746+
</match>
747+
748+
<match>
749+
<premise><c>for i in range(1, 100)</c></premise>
750+
<response><c>for (int i = 1; i &lt; 100; i++)</c></response>
751+
</match>
752+
753+
<match>
754+
<response><c>for (int i = 1; i &lt;= 100; i++)</c></response>
755+
</match>
756+
757+
<match>
758+
<premise><c>for i in range(100, 0, -1)</c></premise>
759+
<response><c>for (int i = 100; i &gt; 0; i--)</c></response>
760+
</match>
761+
762+
<match>
763+
<response><c>for (int i = 2; i &lt;= 102; i += 2)</c></response>
764+
</match>
765+
766+
<match>
767+
<premise><c>for x, y in zip(range(10), range(0, 20, 2))</c></premise>
768+
<response><c>for (int x = 0, y = 0; x &lt; 10; x++, y += 2)</c></response>
769+
</match>
770+
771+
<match>
772+
<response><c>for (int i = 100; i &lt; 0; i--)</c></response>
773+
</match>
774+
775+
<match>
776+
<response><c>for (int x = 0, int y = 0; x &lt; 10; x++, y += 2)</c></response>
777+
</match>
778+
</matches>
779+
</exercise>
762780

763781
<p>
764782
The next loop (lines 27&#x2013;30) shows a typical Java pattern for reading data from a file. Java while loops and Python while loops are identical in their logic. In this case, we will continue to process the body of the loop as long as <c>data.hasNextInt()</c> returns true.

source/ch5_loopsanditeration.ptx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<program xml:id="python-definite-loop" interactive="activecode" language="python">
1919
<code>
20-
for i in range(10):
20+
for i in range(10): # range(10) is a list of integers from 0 to 9
2121
print(i)
2222
</code>
2323
</program>
@@ -30,7 +30,7 @@ for i in range(10):
3030
<code>
3131
public class DefiniteLoopExample {
3232
public static void main(String[] args) {
33-
for (Integer i = 0; i &lt; 10; i++ ) {
33+
for (Integer i = 0; i &lt; 10; i++ ) { // notice how the initialization, condition, and update are all on the same line.
3434
System.out.println(i);
3535
}
3636
}
@@ -67,7 +67,7 @@ public class DefiniteLoopExample {
6767

6868
<program xml:id="python-deinite-decrement" interactive="activecode" language="python">
6969
<code>
70-
for i in range(100, -1, -5):
70+
for i in range(100, -1, -5): # start at 100, stop at 0, decrement by 5
7171
print(i)
7272
</code>
7373
</program>
@@ -80,7 +80,7 @@ for i in range(100, -1, -5):
8080
<code>
8181
public class DefiniteLoopBackward {
8282
public static void main(String[] args) {
83-
for (Integer i = 100; i &gt;= 0; i -= 5) {
83+
for (Integer i = 100; i &gt;= 0; i -= 5) { // start at 100, stop at 0, decrement by 5
8484
System.out.println(i);
8585
}
8686
}
@@ -99,8 +99,8 @@ public class DefiniteLoopBackward {
9999

100100
<program xml:id="python-list-iteration" interactive="activecode" language="python">
101101
<code>
102-
l = [1, 1, 2, 3, 5, 8, 13, 21]
103-
for fib in l:
102+
l = [1, 1, 2, 3, 5, 8, 13, 21] # create a list of integers
103+
for fib in l: # iterate over the list
104104
print(fib)
105105
</code>
106106
</program>
@@ -115,16 +115,16 @@ import java.util.ArrayList;
115115

116116
public class ForEachArrayListExample {
117117
public static void main(String[] args) {
118-
ArrayList&lt;Integer&gt; l = new ArrayList&lt;Integer&gt;();
119-
l.add(1);
120-
l.add(1);
121-
l.add(2);
118+
ArrayList&lt;Integer&gt; l = new ArrayList&lt; // create an ArrayList of integers
119+
l.add(1); // add the first integer to the list
120+
l.add(1); // add the second integer to the list
121+
l.add(2); // keep going
122122
l.add(3);
123123
l.add(5);
124124
l.add(8);
125125
l.add(13);
126-
l.add(21);
127-
for (Integer i : l) {
126+
l.add(21); // add the last integer to the list
127+
for (Integer i : l) { // iterate over the list
128128
System.out.println(i);
129129
}
130130
}
@@ -141,8 +141,8 @@ public class ForEachArrayListExample {
141141
<code>
142142
public class ForEachArrayExample {
143143
public static void main(String[] args) {
144-
int l[] = {1,1,2,3,5,8,13,21};
145-
for(int i : l) {
144+
int l[] = {1,1,2,3,5,8,13,21}; // create an array of integers using primitive syntax
145+
for(int i : l) { // iterate over the array
146146
System.out.println(i);
147147
}
148148
}
@@ -158,8 +158,8 @@ public class ForEachArrayExample {
158158
<code>
159159
public class StringIterationExample {
160160
public static void main(String[] args) {
161-
String t = "Hello World";
162-
for (char c : t.toCharArray()) {
161+
String t = "Hello World"; // create a string
162+
for (char c : t.toCharArray()) { // iterate over the characters in the string
163163
System.out.println(c);
164164
}
165165
}
@@ -181,7 +181,7 @@ public class StringIterationExample {
181181
i = 5
182182
while i &gt; 0: # while i is greater than 0
183183
print(i)
184-
i = i - 1 # decrement i by 1
184+
i = i - 1
185185
</code>
186186
</program>
187187

@@ -214,7 +214,7 @@ public class WhileLoopExample {
214214
<code>
215215
public class DoWhileExample {
216216
public static void main(String[] args) {
217-
int i = 10; // initialize i to 10
217+
int i = 10;
218218
do { // do-while loop, will run at least once no matter the condition
219219
System.out.println("This runs once, i = " + i);
220220
} while (i &lt; 5); // while i is less than 5

0 commit comments

Comments
 (0)