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/ch4_conditionals.ptx
+12-21Lines changed: 12 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -163,27 +163,18 @@ public class ElseIf {
163
163
164
164
<p>
165
165
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>
177
167
178
168
<note>
179
169
<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.
181
171
</p>
182
172
<programlanguage="python">
183
173
<title>Match Case Example</title>
184
174
<code>
185
-
grade = 100 // 10
186
-
def grading(grade):
175
+
grade = 85
176
+
tempgrade = grade // 10
177
+
def grading(tempgrade):
187
178
match grade:
188
179
case 10 | 9:
189
180
return 'A'
@@ -195,7 +186,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
195
186
return 'D'
196
187
case _:
197
188
return 'F'
198
-
print(grading(grade))
189
+
print(grading(tempgrade))
199
190
</code>
200
191
</program>
201
192
</note>
@@ -248,7 +239,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
248
239
<title>Exception Handling</title>
249
240
250
241
<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:
@@ -282,7 +273,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
282
273
</program>
283
274
284
275
<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:
@@ -320,7 +311,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
320
311
break;
321
312
} catch (InputMismatchException e) {
322
313
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
324
315
}
325
316
}
326
317
}
@@ -329,13 +320,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
329
320
</program>
330
321
331
322
<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.
333
324
</p>
334
325
335
326
<p>
336
327
<idx>checked exception</idx>
337
328
<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:
339
330
</p>
340
331
341
332
<table>
@@ -390,7 +381,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
390
381
</table>
391
382
392
383
<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.
0 commit comments