Skip to content

Commit 1938c22

Browse files
committed
add xml:ids to all programs in ch4
1 parent 291413d commit 1938c22

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
In Python the simple <c>if</c> statement is written as:
1717
</p>
1818

19-
<program interactive="activecode" language="python">
19+
<program xml:id="python-simple-if" interactive="activecode" language="python">
2020
<code>
2121
score = 95
2222
if score &gt;= 90:
@@ -26,7 +26,7 @@ if score &gt;= 90:
2626
<p>
2727
In Java, this same pattern requires two changes: the condition must be in parentheses <c>()</c>, and the code block must be enclosed in curly braces <c>{}</c>.
2828
</p>
29-
<program interactive="activecode" language="java">
29+
<program xml:id="java-simple-if" interactive="activecode" language="java">
3030
<code>
3131
public class SimpleIfExample {
3232
public static void main(String[] args) {
@@ -40,15 +40,15 @@ if score &gt;= 90:
4040
</program>
4141
<p>
4242
Once again you can see that in Java the curly braces define a block rather than indentation.
43-
In Java the parenthesis around the condition are required because it is technically a function that evaluates to <c>True</c> or <c>False</c>.
43+
In Java, the parentheses around the condition are required because it is technically a function that evaluates to <c>True</c> or <c>False</c>.
4444
</p>
4545
</section>
4646

47-
<section xml:id="if-else">
47+
<section xml:id="use-the-if-else-statement">
4848
<title>Using the <c>if</c> - <c>else</c> Statement</title>
4949

5050
<p>The Java equivalent follows the same syntactical rules as before.</p>
51-
<program interactive="activecode" language="python">
51+
<program xml:id="python-if-else" interactive="activecode" language="python">
5252
<code>
5353
age = 16
5454
if age &gt;= 18:
@@ -58,7 +58,7 @@ if score &gt;= 90:
5858
</code>
5959
</program>
6060

61-
<program language="java">
61+
<program xml:id="java-if-else" language="java">
6262
<code>
6363
public class IfElseExample {
6464
public static void main(String[] args) {
@@ -84,7 +84,7 @@ if score &gt;= 90:
8484
</p>
8585

8686

87-
<program interactive="activecode" language="python">
87+
<program xml:id="python-if-nesting" interactive="activecode" language="python">
8888
<code>
8989
grade = int(input('enter a grade'))
9090
if grade &lt; 60:
@@ -101,11 +101,11 @@ else:
101101
</program>
102102

103103
<p>
104-
In Java we have a couple of ways to write this.
104+
In Java, we have a couple of ways to write this.
105105
</p>
106106

107107

108-
<program interactive="activecode" language="java">
108+
<program xml:id="java-ifnesting" interactive="activecode" language="java">
109109
<code>
110110
public class ElseIf {
111111
public static void main(String args[]) {
@@ -133,12 +133,12 @@ public class ElseIf {
133133
</program>
134134

135135
<p>
136-
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 if is the only statement used in each else we can get away with the following.
136+
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.
137137
</p>
138138

139139

140140

141-
<program interactive="activecode" language="java">
141+
<program xml:id="java-if-else-no-nesting" interactive="activecode" language="java">
142142
<code>
143143
public class ElseIf {
144144
public static void main(String args[]) {
@@ -167,9 +167,9 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
167167

168168
<note>
169169
<p>
170-
Depending on your knowledge and experience with Python you may already be familiar and questioning why we are not using the <c>match</c> statement in our Python examples. The answer is that this book currently runs its active code examples using Python 3.7, which does not support the <c>match</c> statement which was introduced in Python 3.10. Below is an example of the <c>match</c> statement similar to our grade method.
170+
Depending on your knowledge and experience with Python you may be questioning why we are not using the <c>match</c> statement in our Python examples. The answer is that this book currently runs its active code examples using Python 3.7, which does not support the <c>match</c> statement which was introduced in Python 3.10. Below is an example of the <c>match</c> statement similar to our grade method.
171171
</p>
172-
<program language="python">
172+
<program xml:id="python-match" language="python">
173173
<title>Match Case Example</title>
174174
<code>
175175
grade = 85
@@ -201,7 +201,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
201201
Java 14 introduced <c>switch</c> <term>expressions</term>, enhancing functionality by allowing the <c>switch</c> to return values and eliminating <c>fall-through</c> via the <c>-&gt;</c> arrow syntax. These expressions can even use <c>yield</c> within code blocks for more complex evaluations. <term><c>yield</c></term> is used inside a switch expression’s block to produce the value of that expression, unlike <c>break</c> which simply exits a switch statement or loop. It’s important to note that traditional <c>switch</c> statements do not support <c>null</c> values and will throw a <c>NullPointerException</c> if evaluated with <c>null</c>. As the language evolves, newer versions of Java continue to extend <c>switch</c> capabilities with features like <c>pattern matching</c> and enhanced <c>type handling</c>, making it a more powerful and expressive tool for decision-making in Java programs.
202202
</p>
203203

204-
<program interactive="activecode" language="java">
204+
<program xml:id="java-switch-up" interactive="activecode" language="java">
205205
<code>
206206
public class SwitchUp {
207207
public static void main(String args[]) {
@@ -241,7 +241,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
241241
In Python, if you want a program to continue running when an error has occurred, you can use <c>try-except</c> blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so:
242242
</p>
243243

244-
<program interactive="activecode" language="python" xml:id="square-input-python">
244+
<program xml:id="python-square-input" interactive="activecode" language="python">
245245
<code>
246246
number = int(input("Please enter a whole number: "))
247247
squared = number ** 2
@@ -253,7 +253,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
253253
The Java code that would perform the same task is a little more complex and utilizes the <c>Scanner</c> class for input.
254254
</p>
255255

256-
<program interactive="activecode" language="java" xml:id="square-input-java">
256+
<program xml:id="java-square-input" interactive="activecode" language="java">
257257
<code>
258258
import java.util.Scanner;
259259

@@ -275,7 +275,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
275275
This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add <c>try-except</c> (Python) or <c>try-catch</c> (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding <c>try-except</c> blocks and a <c>while</c> loop to the Python code will look something like this:
276276
</p>
277277

278-
<program interactive="activecode" language="python" xml:id="square-input-exception-python">
278+
<program xml:id="python-square-input-exception" interactive="activecode" language="python">
279279
<code>
280280
while True:
281281
try:
@@ -292,7 +292,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
292292
Now that we have Python code that will continuously prompt the user until they enter a whole number, let's look at Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.
293293
</p>
294294

295-
<program interactive="activecode" language="java" xml:id="square-input-exception-java">
295+
<program xml:id="java-square-input-exception" interactive="activecode" language="java">
296296
<code>
297297
import java.util.Scanner;
298298
import java.util.InputMismatchException;
@@ -439,7 +439,7 @@ of an assignment statement. The following table summarizes how this works:
439439
<p>
440440
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
441441
</p>
442-
<program interactive ="activecode" language ="java">
442+
<program xml:id="java-ternary" interactive ="activecode" language ="java">
443443
<code>
444444
class Main {
445445
public static void main(String[] args) {

0 commit comments

Comments
 (0)