diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 8a9682d..7410fc6 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -4,8 +4,7 @@ Conditionals -
- Using the Simple <c>if</c> Statement +
Using Conditional Statements in Java

conditional statements Conditional statements in Python and Java are very similar. In Python we have three patterns: @@ -50,9 +49,9 @@ if score >= 90: # Note the colon at the end of the line Once again you can see that in Java the curly braces define a block rather than indentation. In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.

-
+ -
+ Using the <c>if</c> - <c>else</c> Statement

shows how the if - elsestatement is written in Python.

@@ -85,9 +84,9 @@ if score >= 90: # Note the colon at the end of the line -
+ -
+ Can we use <c>elif</c>?

elif statement @@ -172,9 +171,9 @@ public class ElseIf { -

+ -
+ Using the <c>switch</c> Statement

@@ -255,9 +254,94 @@ The switch statement is not used very often, and we recommend you do not

Finally, the switch statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the elif. Even with the new features of Java 14+ the switch statement is still limited to constant comparisons using equality.

+
-
+
+ The Ternary Operator + +

Boolean operators simple comparisons compound Boolean expressions +The conditionals used in the if statement can be Boolean variables, simple comparisons, and compound Boolean expressions. +

+ +

ternary operator +Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. summarizes how it works. +

+ + + Ternary Operator in Java + + + Component + Description + + + condition + The boolean expression that is evaluated (e.g., a % 2 == 0). + + + ? + This is the ternary operator that separates the condition from the trueValue. + + + trueValue + The value assigned if the condition is true (e.g., a * a). + + + : + This is the ternary operator that separates the trueValue from the falseValue. + + + falseValue + The value assigned if the condition is false (e.g., 3 * x - 1). + + + Example Usage + a = a % 2 == 0 ? a * a : 3 * x - 1 + + + Equivalent if-else Code + Can also be written with a regular if-else statement, but the ternary form is more concise. + + +
+ +

+Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. shows an example where we see the same logic implemented in two different ways. +

+ + + +public class Ternary { + public static void main(String[] args) { + int a = 4; + int x = 2; + int outp; + + // ternary: + outp = (a % 2 == 0) ? (a * a) : (3 * x - 1); + System.out.println("ternary result: " + outp); + + // Equivalent using if/else + if (a % 2 == 0) { + outp = a * a; + } else { + outp = 3 * x - 1; + } + + System.out.println("if/else result: " + outp); + } +} + + + + +

+ In , we are using this ternary operator to assign a value to a based on whether a is even or odd. If a is even, it will be squared; if odd, it will be instead be calculated as 3 * x - 1. This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions. +

+
+ +
Exception Handling

@@ -411,92 +495,6 @@ The switch statement is not used very often, and we recommend you do not Note that as with other structures in Java, try-catch blocks blocks must be encased with braces {}. The most important part of this code is, after catch, there is a set of parenthesis with an exception type and a variable name catch (InputMismatchException e). This is where we declare a InputMismatchException exception and name it with the variable name e. It is common practice, though not a requirement, to name exception variables e in this manner.

-
- -
- The Ternary Operator - -

Boolean operators simple comparisons compound Boolean expressions -The conditionals used in the if statement can be Boolean variables, simple comparisons, and compound Boolean expressions. -

- -

ternary operator -Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. summarizes how it works. -

- - - Ternary Operator in Java - - - Component - Description - - - condition - The boolean expression that is evaluated (e.g., a % 2 == 0). - - - ? - This is the ternary operator that separates the condition from the trueValue. - - - trueValue - The value assigned if the condition is true (e.g., a * a). - - - : - This is the ternary operator that separates the trueValue from the falseValue. - - - falseValue - The value assigned if the condition is false (e.g., 3 * x - 1). - - - Example Usage - a = a % 2 == 0 ? a * a : 3 * x - 1 - - - Equivalent if-else Code - Can also be written with a regular if-else statement, but the ternary form is more concise. - - -
- -

-Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. shows an example where we see the same logic implemented in two different ways. -

- - - -public class Ternary { - public static void main(String[] args) { - int a = 4; - int x = 2; - int outp; - - // ternary: - outp = (a % 2 == 0) ? (a * a) : (3 * x - 1); - System.out.println("ternary result: " + outp); - - // Equivalent using if/else - if (a % 2 == 0) { - outp = a * a; - } else { - outp = 3 * x - 1; - } - - System.out.println("if/else result: " + outp); - } -} - - - - -

- In , we are using this ternary operator to assign a value to a based on whether a is even or odd. If a is even, it will be squared; if odd, it will be instead be calculated as 3 * x - 1. This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions. -

- -
Summary & Reading Questions