Skip to content

Commit 6e2388a

Browse files
committed
remove stray code and some minor fixes
1 parent 282c172 commit 6e2388a

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,18 @@ public class ElseIf {
163163

164164
<p>
165165
Java also supports a <c>switch</c> statement that acts something like the <c>elif</c> statement of Python under certain conditions. To write the grade program using a <c>switch</c> statement we would use the following:
166-
</p>while True:
167-
168-
try:
169-
170-
x = int(input("Please enter a whole number: "))
171-
172-
break
173-
174-
except ValueError:
175-
176-
print("Oops! That was no valid number. Try again...")
166+
</p>
177167

178168
<note>
179169
<p>
180-
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 on Python 3.7, which does not support the <c>match</c> statement. The <c>match</c> statement 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 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.
181171
</p>
182172
<program language="python">
183173
<title>Match Case Example</title>
184174
<code>
185-
grade = 100 // 10
186-
def grading(grade):
175+
grade = 85
176+
tempgrade = grade // 10
177+
def grading(tempgrade):
187178
match grade:
188179
case 10 | 9:
189180
return 'A'
@@ -195,7 +186,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
195186
return 'D'
196187
case _:
197188
return 'F'
198-
print(grading(grade))
189+
print(grading(tempgrade))
199190
</code>
200191
</program>
201192
</note>
@@ -248,7 +239,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
248239
<title>Exception Handling</title>
249240

250241
<p>
251-
In Python, if you want a program to continue running when an error has occurred, you can use try/except 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:
242+
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:
252243
</p>
253244

254245
<program interactive="activecode" language="python" xml:id="square-input-python">
@@ -282,7 +273,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
282273
</program>
283274

284275
<p>
285-
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 try/except (Python) or try/catch (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding try/except blocks and a <c>while</c> loop to the Python code will look something like this:
276+
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:
286277
</p>
287278

288279
<program interactive="activecode" language="python" xml:id="square-input-exception-python">
@@ -320,7 +311,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
320311
break;
321312
} catch (InputMismatchException e) {
322313
System.out.println("That was not a valid number. Please try again: ");
323-
scanner.nextLine();
314+
scanner.nextLine(); // Clear the invalid input from the scanner
324315
}
325316
}
326317
}
@@ -329,13 +320,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
329320
</program>
330321

331322
<p>
332-
Firstly, let's talk about the extra import alongside the <c>Scanner</c> import. In Java, any exception that isn't part of the <c>java.lang</c> class or is a checked exception must be explicitly imported to be used with try/catch blocks. If you ran the previous Java codeblock without try/catch blocks and entered an erroneous input, you would have got an <c>InputMismatchException</c> exception despite not having imported this class. That being said, removing the explicit import of this library for the try/catch code block above will lead to compilation errors.
323+
Firstly, let's talk about the extra import alongside the <c>Scanner</c> import. In Java, we need to import <c>InputMismatchException</c> because it's not automatically available like basic exceptions. This is different from Python where most exceptions are readily accessible. If you ran the previous Java codeblock without <c>try-catch</c> blocks and entered an erroneous input, you would have got an <c>InputMismatchException</c> exception despite not having imported this class. That being said, removing the explicit import of this library for the <c>try-catch</c> code block above will lead to compilation errors.
333324
</p>
334325

335326
<p>
336327
<idx>checked exception</idx>
337328
<idx>unchecked exception</idx>
338-
Exceptions in Java fall under two categories: checked and unchecked. <term>Checked exceptions</term> must be explicitly imported and declared along with try/catch blocks for a program to compile. <term>Unchecked exceptions</term> do not need to be imported unless try/catch blocks are implemented for them (except for <c>java.lang</c> exceptions). <c>InputMismatchException</c> is an unchecked exception that is not part of the <c>java.lang</c> library, so it is only included if try/catch blocks declare it. Here are some common exceptions used with try/catch blocks:
329+
Exceptions in Java fall under two categories: checked and unchecked. <term>Checked exceptions</term> must be explicitly imported and declared along with <c>try-catch</c> blocks for a program to compile. <term>Unchecked exceptions</term> do not need to be imported unless <c>try-catch</c> blocks are implemented for them (except for <c>java.lang</c> exceptions). <c>InputMismatchException</c> is an unchecked exception that is not part of the <c>java.lang</c> library, so it is only included if <c>try-catch</c> blocks declare it. Here are some common exceptions used with <c>try-catch</c> blocks:
339330
</p>
340331

341332
<table>
@@ -390,7 +381,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
390381
</table>
391382

392383
<p>
393-
Next, we will look into the try/catch blocks themselves. As with most other structures in Java, the blocks must be encased with braces <c>{}</c>. The most important part of this code is, after <c>catch</c>, there is a set of parenthesis with an exception type and a variable name <c>catch (InputMismatchException e)</c>. This is where we declare a <c>InputMismatchException</c> exception and name it with the variable name <c>e</c>. It is common practice to name exception variables <c>e</c> in this manner.
384+
Next, we will look into the <c>try-catch</c> blocks themselves. As with most other structures in Java, the blocks must be encased with braces <c>{}</c>. The most important part of this code is, after <c>catch</c>, there is a set of parenthesis with an exception type and a variable name <c>catch (InputMismatchException e)</c>. This is where we declare a <c>InputMismatchException</c> exception and name it with the variable name <c>e</c>. It is common practice to name exception variables <c>e</c> in this manner.
394385
</p>
395386

396387
</section>

0 commit comments

Comments
 (0)