Boolean values: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
(→{{header|Phix}}: added p2js note) |
||
Line 1,972: | Line 1,972: | ||
A boolean test is inverted by preceding it with the keyword <code>not</code>. The null character ('\0') is considered false, all other characters are deemed true. |
A boolean test is inverted by preceding it with the keyword <code>not</code>. The null character ('\0') is considered false, all other characters are deemed true. |
||
The builtin constants TRUE/FALSE and their aliases True/true/False/false may also be used. |
The builtin constants TRUE/FALSE and their aliases True/true/False/false may also be used. |
||
There is however a gotcha in JavaScript, and hence pwa/p2js, in that <b>true !== 1</b> and <b>false !== 0</b>. In almost all other respects, true is 1 and false is 0, for instance 1+true evaluates to 2, just like desktop/Phix. It is only direct comparison for equality of booleans and numbers using an infix operator which fails, and I suppose you could argue that is a programming logic blunder (by which I mean in a particular source file of a specific application, rather than in the programming language definition). There is no such issue with equal() and compare(), which of course can be used instead, and pwa/p2js automatically maps to when needed, that is except for bool vs number, which is difficult because in desktop/Phix <i>those are the same thing</i>. Thankfully, there are very few places anyone ever actually compares bools against 0 and 1 using an infix operator. |
|||
The following example illustrates, and also emphasies the subtlety of the issue (no difference whatsoever if c, d, e, f are defined as bool): |
|||
<!--<lang Phix>(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">==</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- fine</span> |
|||
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">==</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- oops</span> |
|||
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">==</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- fine</span> |
|||
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- fine, ditto equal(c,true)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d==2:%5t(%d) ==1:%5t, eq1:%5t, ==true:%5t\n"</span><span style="color: #0000FF;">,</span> |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #000080;font-style:italic;">-- |
|||
-- output on desktop/Phix: 1==2:false(0) ==1:false, eq1:false, ==true:false |
|||
-- 2==2: true(1) ==1: true, eq1: true, ==true: true |
|||
-- 3==2:false(0) ==1:false, eq1:false, ==true:false |
|||
-- |
|||
-- output on pwa/p2js: 1==2:false(0) ==1:false, eq1:false, ==true:false |
|||
-- 2==2: true(1) ==1:<font color="#FF0000">false</font>, eq1: true, ==true: true |
|||
-- 3==2:false(0) ==1:false, eq1:false, ==true:false |
|||
--</span> |
|||
<!--</lang>--> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |