Skip to content

Commit 964581e

Browse files
committed
added a match interactive question
1 parent 296cd38 commit 964581e

1 file changed

Lines changed: 26 additions & 29 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -729,36 +729,33 @@ 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>
742-
743-
<li>
744-
<p>
745-
<c>for i in range(1,100)</c>
746-
</p>
747-
</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>
748733

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+
<matches>
741+
<match>
742+
<premise><c>for i in range(2, 101, 2)</c></premise>
743+
<response><c>for (int i = 2; i &lt; 101; i += 2)</c></response>
744+
</match>
745+
<match>
746+
<premise><c>for i in range(1, 100)</c></premise>
747+
<response><c>for (int i = 1; i &lt; 100; i++)</c></response>
748+
</match>
749+
<match>
750+
<premise><c>for i in range(100, 0, -1)</c></premise>
751+
<response><c>for (int i = 100; i &gt; 0; i--)</c></response>
752+
</match>
753+
<match>
754+
<premise><c>for x, y in zip(range(10), range(0, 20, 2))</c></premise>
755+
<response><c>for (int x = 0, y = 0; x &lt; 10; x++, y += 2)</c></response>
756+
</match>
757+
</matches>
758+
</exercise>
762759

763760
<p>
764761
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.

0 commit comments

Comments
 (0)