Jump to content

Short-circuit evaluation: Difference between revisions

Add Ecstasy example
(Added Quackery.)
(Add Ecstasy example)
Line 1,179:
<syntaxhighlight lang="e">def x := a(i) && (def funky := b(j))</syntaxhighlight>
The choice we make is that <code>funky</code> is ordinary if the right-side expression was evaluated, and otherwise is <em>ruined</em>; attempts to access the variable give an error.
 
=={{header|Ecstasy}}==
Similar to Java, Ecstasy uses the `&&` and `||` operators for short-circuiting logic, and `&` and `|` are the normal (non-short-circuiting) forms.
 
<syntaxhighlight lang="java">
module test
{
@Inject Console console;
 
static Boolean show(String name, Boolean value)
{
console.print($"{name}()={value}");
return value;
}
 
void run()
{
val a = show("a", _);
val b = show("b", _);
 
for (Boolean v1 : False..True)
{
for (Boolean v2 : False..True)
{
console.print($"a({v1}) && b({v2}) == {a(v1) && b(v2)}");
console.print();
console.print($"a({v1}) || b({v2}) == {a(v1) || b(v2)}");
console.print();
}
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
a()=False
a(False) && b(False) == False
 
a()=False
b()=False
a(False) || b(False) == False
 
a()=False
a(False) && b(True) == False
 
a()=False
b()=True
a(False) || b(True) == True
 
a()=True
b()=False
a(True) && b(False) == False
 
a()=True
a(True) || b(False) == True
 
a()=True
b()=True
a(True) && b(True) == True
 
a()=True
a(True) || b(True) == True
</pre>
 
=={{header|Elena}}==
162

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.