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
{{ message }}
Repository navigation
Commit 945cc61
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/ch_5_conditionals.ptx
+67-1Lines changed: 67 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -239,8 +239,74 @@ The <c>switch</c> statement is not used very often, and I recommend you do not u
239
239
The conditionals used in the if statement can be boolean variables, simple comparisons, and compound boolean expressions.
240
240
</p>
241
241
242
+
<p>
243
+
Java also supports the <c>boolean</c> expression using the ternary operator
244
+
<c>condition ? trueValue : falseValue</c>. This operator tests a condition as part
245
+
of an assignment statement. The following table summarizes how this works:
246
+
</p>
247
+
248
+
<table>
249
+
<title>Ternary Operator in Java</title>
250
+
<tabular>
251
+
<row>
252
+
<cell><term>Component</term></cell>
253
+
<cell><term>Description</term></cell>
254
+
</row>
255
+
<row>
256
+
<cell><c>condition</c></cell>
257
+
<cell>The boolean expression that is evaluated (e.g., <c>a % 2 == 0</c>).</cell>
258
+
</row>
259
+
<row>
260
+
<cell><c>?</c></cell>
261
+
<cell> This is the ternary operator that separates the condition from the <c>trueValue</c>.</cell>
262
+
</row>
263
+
<row>
264
+
<cell><c>trueValue</c></cell>
265
+
<cell>The value assigned if the condition is true (e.g., <c>a * a</c>).</cell>
266
+
</row>
267
+
<row>
268
+
<cell><c>:</c></cell>
269
+
<cell> This is the ternary operator that separates the <c>trueValue</c> from the <c>falseValue</c>.</cell>
270
+
</row>
271
+
<row>
272
+
<cell><c>falseValue</c></cell>
273
+
<cell>The value assigned if the condition is false (e.g., <c>3 * x - 1</c>).</cell>
274
+
</row>
275
+
<row>
276
+
<cell>Example Usage</cell>
277
+
<cell><c>a = a % 2 == 0 ? a * a : 3 * x - 1</c></cell>
278
+
</row>
279
+
<row>
280
+
<cell>Equivalent if-else Code</cell>
281
+
<cell>Can also be written with a regular <c>if-else</c> statement, but the ternary form is more concise.</cell>
282
+
</row>
283
+
</tabular>
284
+
</table>
285
+
286
+
<p>
287
+
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
288
+
</p>
289
+
<program interactive ="activecode" language ="java">
290
+
<code>
291
+
class Main {
292
+
public static void main(String[] args) {
293
+
int a = 4;
294
+
int x = 2;
295
+
296
+
// Using the ternary operator
297
+
a = (a % 2 == 0) ? a * a : 3 * x - 1;
298
+
299
+
System.out.println("Result: " + a);
300
+
}
301
+
}
302
+
303
+
</code>
304
+
</program>
305
+
242
306
<p>
243
-
Java also supports the boolean expression. <c>condition ? trueValue : falseValue</c> This expression can be used to test a condition as part of an assignment statement. For example <c>a = a % 2 == 0 ? a*a : 3*x -1</c> In the previous assignment statement the expression <c>a%2 ==0</c> is first checked. If it is true then a is assigned the value <c>a * a</c> if it is false then a is assigned the value of <c>3*x-1</c>. Of course all of this could have been accomplished using a regular if else statement, but sometimes the convenience of a single statement is too much to resist.
307
+
In this example we are using this ternary operator to assign a value to <c>a</c> based on wether <c>a</c> is even or odd. If <c>a</c> is even, it will be squared; if odd, it will be instead be calculated as <c>3 * x - 1</c>. This is a concise way to write conditional assignments in Java. However, it should be used reasonably, as it can make code less readable if overused or used in complex expressions.
0 commit comments