Skip to content

Commit 945cc61

Browse files
committed
Added table in binary, added example and explenation, fixes issue RunestoneInteractive#60
1 parent 3e33863 commit 945cc61

1 file changed

Lines changed: 67 additions & 1 deletion

File tree

‎source/ch_5_conditionals.ptx‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,74 @@ The <c>switch</c> statement is not used very often, and I recommend you do not u
239239
The conditionals used in the if statement can be boolean variables, simple comparisons, and compound boolean expressions.
240240
</p>
241241

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+
242306
<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.
244308
</p>
309+
310+
245311
</section>
246312
</chapter>

0 commit comments

Comments
 (0)