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/ch3_javadatatypes.ptx
+47-29Lines changed: 47 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -729,36 +729,54 @@ public class Histo {
729
729
</p>
730
730
731
731
<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 < 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 < 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>
742
733
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
+
<exerciselabel="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 < 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 < 100; i++)</c></response>
751
+
</match>
752
+
753
+
<match>
754
+
<response><c>for (int i = 1; i <= 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 > 0; i--)</c></response>
760
+
</match>
761
+
762
+
<match>
763
+
<response><c>for (int i = 2; i <= 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 < 10; x++, y += 2)</c></response>
769
+
</match>
770
+
771
+
<match>
772
+
<response><c>for (int i = 100; i < 0; i--)</c></response>
773
+
</match>
774
+
775
+
<match>
776
+
<response><c>for (int x = 0, int y = 0; x < 10; x++, y += 2)</c></response>
777
+
</match>
778
+
</matches>
779
+
</exercise>
762
780
763
781
<p>
764
782
The next loop (lines 27–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.
0 commit comments