Conditional structures: Difference between revisions
Content added Content deleted
Tag: Undo |
(→{{header|Java}}: Added switch expressions) |
||
Line 3,411: | Line 3,411: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
===if-then-else=== |
===if-then-else=== |
||
<syntaxhighlight lang="java">if(s.equals("Hello World")) |
<syntaxhighlight lang="java">if (s.equals("Hello World")) { |
||
{ |
|||
foo(); |
foo(); |
||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
|||
deusEx(); |
deusEx(); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Java also supports [[wp:Short-circuit_evaluation|short-circuit evaluation]]. So in a conditional like this: |
Java also supports [[wp:Short-circuit_evaluation|short-circuit evaluation]]. So in a conditional like this: |
||
<syntaxhighlight lang="java">if(obj != null && obj.foo()){ |
<syntaxhighlight lang="java">if (obj != null && obj.foo()) { |
||
aMethod(); |
aMethod(); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
<tt>obj.foo()</tt> will not be executed if <tt>obj != null</tt> returns false. It is possible to have conditionals without short circuit evaluation using the <tt>&</tt> and <tt>|</tt> operators (from [[Bitwise operations]]). So in this conditional: |
<tt>obj.foo()</tt> will not be executed if <tt>obj != null</tt> returns false. It is possible to have conditionals without short circuit evaluation using the <tt>&</tt> and <tt>|</tt> operators (from [[Bitwise operations]]). So in this conditional: |
||
<syntaxhighlight lang="java">if(obj != null & obj.foo()){ |
<syntaxhighlight lang="java">if (obj != null & obj.foo()) { |
||
aMethod(); |
aMethod(); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
You will get a null pointer exception if obj is null. |
You will get a null pointer exception if <tt>obj</tt> is null. |
||
===ternary=== |
===ternary=== |
||
<syntaxhighlight lang="java">s.equals("Hello World") ? foo() : bar();</syntaxhighlight> |
<syntaxhighlight lang="java">s.equals("Hello World") ? foo() : bar();</syntaxhighlight> |
||
The ternary operator is an expression, and is most often used as such: |
|||
<syntaxhighlight lang="java">Object newValue = s.equals("Hello World") ? a : b;</syntaxhighlight> |
|||
===switch=== |
===switch=== |
||
This structure will only work if the code being switched on evaluates to an integer or character. There is no switching on |
This structure will only work if the code being switched on evaluates to an integer or character. There is no switching on <code>Object</code>s (except for Java 17 and higher), <code>long</code>s, or floating-point types in Java (except for <code>String</code>s and enum types in Java 7 and higher). |
||
<syntaxhighlight lang="java">switch(c) { |
<syntaxhighlight lang="java">switch (c) { |
||
case 'a': |
case 'a': |
||
foo(); |
foo(); |
||
break; |
break; |
||
case 'b': |
case 'b': |
||
bar(); |
bar(); |
||
default: |
default: |
||
foobar(); |
foobar(); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
This particular example can show the "fallthrough" behavior of a switch statement. If c is the character b, then bar() and foobar() will both be called. If c is the character a, only foo() will be called because of the break statement at the end of that case. |
This particular example can show the "fallthrough" behavior of a switch statement. If c is the character b, then bar() and foobar() will both be called. If c is the character a, only foo() will be called because of the break statement at the end of that case. |
||
Also, the switch statement can be easily translated into an |
Also, the switch statement can be easily translated into an if-else statement. The example above is equivalent to: |
||
<syntaxhighlight lang="java">if(c == 'a'){ |
<syntaxhighlight lang="java">if (c == 'a') { |
||
foo(); |
foo(); |
||
}else if(c == 'b'){ |
} else if (c == 'b') { |
||
bar(); |
bar(); |
||
foobar(); |
foobar(); |
||
}else{ |
} else { |
||
foobar(); |
foobar(); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Cases without breaks at the end require duplication of code for all cases underneath them until a break is found (like the else if block shown here). |
Cases without breaks at the end require duplication of code for all cases underneath them until a break is found (like the else if block shown here). |
||
===switch expressions=== |
|||
{{works with|Java|14+}} |
|||
Switch statements can be expressions. They must be exhaustive, and return a value with the <tt>yield</tt> keyword. |
|||
<syntaxhighlight lang=“java”>int x = switch (c) { |
|||
case 'a': |
|||
foo(); |
|||
yield 1; |
|||
case 'b': |
|||
bar(); |
|||
default: |
|||
foobar(); |
|||
yield 0; |
|||
}</syntaxhighlight> |
|||
There is also a new syntax, available for both statements and expressions, that does not use fallthrough: |
|||
<syntaxhighlight lang=“java”>int y = switch (c) { |
|||
case '1', '2' -> 1 // multiple cases can be on one line |
|||
default -> { // use a block for multiple statements |
|||
foobar(); |
|||
yield 0; |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |