Skip to content

Commit cfd3457

Browse files
authored
Merge pull request #266 from habibasorour/issue_252_listing
Issue 252 listing
2 parents 75ce01a + 4630f29 commit cfd3457

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ if score >= 90: # Note the colon at the end of the line
9393
<p><idx><c>elif</c> statement</idx>
9494
Java does not have an <c>elif</c> pattern like Python.
9595
In Java you can get the functionality of an <c>elif</c> statement by nesting <c>if</c> and <c>else</c>.
96-
Here is a simple example in both Python and Java.
96+
Here is a simple example in both Python and Java. <xref ref="python-if-nesting" text="type-global"/> shows how it is written in Python.
9797
</p>
9898

99-
100-
<program xml:id="python-if-nesting" interactive="activecode" language="python">
99+
<listing xml:id="python-if-nesting">
100+
<program interactive="activecode" language="python">
101101
<code>
102102
grade = int(input('enter a grade'))
103-
if grade &lt; 60:
103+
if grade &lt; 60:
104104
print('F')
105105
elif grade &lt; 70:
106106
print('D')
@@ -112,13 +112,14 @@ else:
112112
print('A')
113113
</code> <tests> </tests>
114114
</program>
115+
</listing>
115116

116117
<p>
117-
In Java, we have a couple of ways to write this.
118+
In Java, we have a couple of ways to write this. <xref ref="java-ifnesting" text="type-global"/> shows one way.
118119
</p>
119120

120-
121-
<program xml:id="java-ifnesting" interactive="activecode" language="java">
121+
<listing xml:id="java-ifnesting">
122+
<program interactive="activecode" language="java">
122123
<code>
123124
public class ElseIf {
124125
public static void main(String args[]) {
@@ -144,14 +145,15 @@ public class ElseIf {
144145
}
145146
</code> <tests> </tests>
146147
</program>
148+
</listing>
147149

148150
<p>
149-
We can get even closer to the <c>elif</c> statement by taking advantage of the Java rule that a single statement does not need to be enclosed in curly braces. Since the <c>if</c> is the only statement used in each <c>else</c> block, we can get away with the following.
151+
We can get even closer to the <c>elif</c> statement by taking advantage of the Java rule that a single statement does not need to be enclosed in curly braces. Since the <c>if</c> is the only statement used in each <c>else</c> block, we can get away with <xref ref="java-if-else-no-nesting" text="type-global"/>.
150152
</p>
151153

152154

153-
154-
<program xml:id="java-if-else-no-nesting" interactive="activecode" language="java">
155+
<listing xml:id="java-if-else-no-nesting" >
156+
<program interactive="activecode" language="java">
155157
<code>
156158
public class ElseIf {
157159
public static void main(String args[]) {
@@ -169,6 +171,7 @@ public class ElseIf {
169171
}
170172
</code> <tests> </tests>
171173
</program>
174+
</listing>
172175
</section>
173176

174177
<section xml:id="switch">

0 commit comments

Comments
 (0)