Skip to content

Commit 6909dcf

Browse files
committed
addlisting to code block
1 parent fbb435e commit 6909dcf

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

source/ch4_conditionals.ptx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ Java also provides the ternary operator <c>condition ? valueIfTrue : valueIfFals
460460
<p>
461461
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. See the following as an example where we see the same logic implemented in two different ways.
462462
</p>
463-
<program xml:id="java-ternary" interactive="activecode" language ="java">
463+
<listing xml:id="java-ternary">
464+
<program interactive="activecode" language ="java">
464465
<code>
465466
public class Ternary {
466467
public static void main(String[] args) {
@@ -484,6 +485,7 @@ public class Ternary {
484485
}
485486
</code>
486487
</program>
488+
</listing>
487489

488490
<p>
489491
In this example we are using this ternary operator to assign a value to <c>a</c> based on whether <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, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions.

0 commit comments

Comments
 (0)