Boolean values: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}: bandage dead link with a webarchive reference) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17:
=={{header|360 Assembly}}==
The are no TRUE or FALSE constants in 360 Assembly; but an often used convention is :
<
TRUE DC X'FF'</
=={{header|6502 Assembly}}==
There are no built-in true or false constants, but the functionality can be easily replicated with zero or nonzero values (or any two values which can cause a mutually exclusive branch condition, such as with <code>#$7f</code> and <code>#$80</code> and using <code>BMI</code>/<code>BPL</code>)
Line 28:
A single bit represents true or false. By convention, 0 (cleared) is false, 1 (set) is true.
In the following, "bit" represents the direct address of any of the 256 directly accessible bits.
<
setb bit ; sets</
=={{header|8th}}==
Line 36:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program boolean.s */
Line 87:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|ACL2}}==
Line 93:
=={{header|Action!}}==
<
PrintF("Variable v has value %B%E",v)
IF v THEN
Line 122:
Test(1)
Test(86)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Boolean_values.png Screenshot from Atari 8-bit computer]
Line 147:
=={{header|Ada}}==
[[Ada]] has a predefined discrete type with the specification:
<
with Boolean lattice and relational operations defined on it. See [http://www.adaic.org/standards/1zrm/html/RM-A-1.html RM A.1].
Line 158:
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - note: null char is missing, AND the C generated is won't compile, so some conversions are missing from RS}}
ALGOL 68 Enforces strong typing and so has few default coercions. The appropriate operators must be used to convert to and from '''bool'''[ean] and the following code demonstrates principle conversions:
<
[]BOOL ft = (f, t);
STRING or = " or ";
Line 187:
print(("string: """, string , """ => ", string /= "", or, UPB string /= 0, new line));
print((new line))
OD</
{{out}}
Line 221:
=={{header|APL}}==
0 and 1 are used for boolean types in APL (as in J below).
<
1 ^ 1
1
1 ^ 0
0
</syntaxhighlight>
=={{header|AppleScript}}==
AppleScript has built-in boolean keywords <code>true</code> and <code>false</code>. Numbers do not work in place of boolean expressions, but they do coerce to and from.
<
not false --> true
Line 237:
--> {1, 0, true, false}
true = 1 --> false</
AppleScript also has constants <code>yes</code> and <code>no</code>, which coerce easily to boolean values. They have little practical value in AppleScript except if one wishes to use them as arguments in place of boolean values for novelty's sake. They are interchangeable with boolean values as parameters in AppleScriptObjC (not demonstrated here).
<
--> {true, false}</
<code>yes</code> and <code>no</code> do not coerce to integer values.
Line 248:
Finally, AppleScript also includes keywords <code>with</code> and <code>without</code>, used in declaring parameters for and sending parameters of boolean nature to handlers. They are synonymous with <code>true</code> and <code>false</code>, respectively, and the compiler will sometimes perform the substitution at compile time.
<
gets compiled immediately to become:
<syntaxhighlight lang
However, the equivalent call to the handler utilising <code>yes</code>, whilst accepted readily in place of its boolean counterpart, is left alone by the compiler:
<
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
/* program areaString.s */
Line 327:
</syntaxhighlight>
=={{header|Arturo}}==
<
b: false
Line 336:
if? b -> print "nope"
else -> print "yep"</
{{out}}
Line 358:
In the following example we use zero for false, and one for true to assign boolean values. However, this is just a convention, so other values may also have been used:
<
# Do not put quotes round the numeric values, or the tests will fail
a = 1 # True
Line 375:
if (b != 0) { print "eighth test b is true" } # This should not print
}</
=={{header|Axe}}==
Line 383:
Most BASICs have no keywords for true and false. Boolean expressions evaluate to 0 when false, and a non-zero value (traditional versions of basic use a value of one, although some variants use a value of negative one) when true. Numbers also work in place of boolean expressions following those rules.
<
20 LET B%=NOT(A%)
30 PRINT "THIS VERSION OF BASIC USES"
Line 393:
90 IF A%<>0 THEN PRINT "TEST FIVE (TRUE BY COMPARISON) DOES NOT PRINT"
100 IF B%<>0 THEN PRINT "TEST SIX (TRUE BY COMPARISON) DOES PRINT"
110 END</
==={{header|Applesoft BASIC}}===
IF statement condition treats any non-zero numeric value as true. Comparison operators evaluate to 1 (true) or 0 (false).
Examples:<
? 2 = 2
IF 7 THEN ?"HELLO"</
{{out}}
Line 407:
==={{header|BaCon}}===
<
a = TRUE
b = FALSE
Line 414:
IF 0 THEN PRINT "TRUE" : ELSE PRINT "FALSE"
IF 1 THEN PRINT "TRUE"
IF 2 THEN PRINT "TRUE"</
{{out}}
Line 430:
==={{header|BASIC256}}===
<
' BASIC256 used numbers to represent true and false
' values. Zero is false and anything else is true.
Line 438:
print false
print true
</syntaxhighlight>
==={{header|BBC BASIC}}===
<
REM FALSE and TRUE equate to 0 and -1 (&FFFFFFFF) respectively:
PRINT FALSE
PRINT TRUE</
==={{header|Commodore BASIC}}===
Commodore BASIC evaluates any non-zero number for TRUE—but is typically represented as 16-bit signed integer value of -1 or $FFFF—and zero evaluates to FALSE.
<
15 print chr$(147);chr$(14);
20 print "True is evaulated as:";t%
Line 460:
80 if i then print "True":goto 100
90 print "False"
100 next</
{{out}}
Line 493:
Sample code:
<
Dim i As Integer = 23
Line 506:
i = CInt(true)
Print i
Sleep</
{{out}}
Line 522:
==={{header|Microsoft Small Basic}}===
Microsoft Small Basic has two constants: <code>"True"</code> and <code>"False"</code>.<br>
<
notc = "False"
Else
notc = "True"
EndIf</
==={{header|PowerBASIC}}===
Line 532:
In addition to what's noted above under [[#BASIC|BASIC]], [[PowerBASIC for Windows]] and [[PowerBASIC Console Compiler]] have the <code>ISTRUE</code> and <code>ISFALSE</code> functions. According to the help file, they "return the logical truth or falsity of a given expression". ([[PowerBASIC]] lacks a boolean data type, so the usual practice is to use integers in [[PB/DOS]], and longs in [[PB/CC]] and [[PB/Win]].)
<
x = ISTRUE(1 = 1) ' returns -1
x = ISTRUE(1 = 0) ' returns 0
x = ISFALSE(1 = 1) ' returns 0
x = ISFALSE(1 = 0) ' returns -1</
==={{header|PureBasic}}===
Line 544:
QBasic, QuickBASIC, VB-DOS and GW-BASIC doesn't have a Boolean type. What it does is to take 0 as False, and any other value as True. The easiest way in QBASIC, QuickBASIC and VB-DOS is to create False and True constants of type Integer and, then, use them as needed:
<
CONST FALSE=0
CONST TRUE = Not FALSE
Line 553:
In GW-BASIC you can create a variable called FALSE% and other called TRUE% and do the same. Nevertheless, is more sure to create functions in order to avoid the value to be manipulated:
<
10 DEF FNFALSE = 0
20 DEF FNTRUE = NOT FNFALSE
Line 562:
==={{header|Run BASIC}}===
Basically 0 is false and 1 is true
<
if not(0) then print "0 is false"
if 1 < 2 then print "1 < 2 TRUE"
if 2 > 1 then print "2 > 1 TRUE"
if not(2 < 1) then print "2 not < 1"
if not(1 = 0) then print "1 not = 0"</
==={{header|smart BASIC}}===
Line 580:
==={{header|True BASIC}}===
<
!True BASIC maneja correctamente las expresiones booleanas,
!Pero no tiene un tipo booleano.
Line 596:
END
</syntaxhighlight>
{{out}}
<pre>
Line 607:
==={{header|uBasic/4tH}}===
In conditionals, zero is false, non-zero is true. Note that '''=''' is not only used for assignment, it is also a fully qualified logical operator, so it is easy to assign a true boolean to a variable.
<syntaxhighlight lang=text>t = 1 = 1
f = 0 = 1
Print "False = ";f;", True = ";t</
{{out}}
<pre>False = 0, True = 1
Line 617:
==={{header|Yabasic}}===
<
// Yabasic usa números para representar los valores true y false
// Las constantes incorporadas true y false representan uno y cero respectivamente.
Line 627:
print true
end
</syntaxhighlight>
==={{header|Visual Basic}}===
Line 633:
<br>When converting an integer to a boolean, <code>0</code> is <code>False</code> and anything not equal to <code>0</code> is
<
x = IIf(Int(Rnd * 2), True, False)
MsgBox x</
=={{header|Batch File}}==
Line 643:
You can make a variable false by clearing its value <code>set "var="</code>.
<
@echo off
Line 663:
pause>nul
</syntaxhighlight>
'''Output:'''
Line 717:
So, when applied to <code>bool</code>, we have a <code>bool?</code> type that supports 3 values: <code>true</code>, <code>false</code> and <code>null</code>. This can be useful for some applications where the value can be undefined or missing.
<
Unlike C/C++, there is no conversion in C# between other types and <code>Boolean</code>.
Line 733:
The standard library defines a data type <code>Bool</code>, which has exactly two members:
<
In addition to all the functionality of any other Clean algebraic data type (e.g. [[pattern matching]]), and the specified derived typeclass instances, the built-in guard (“<code>|</code>”) and <code>if</code> syntaxes use Bool.
Line 743:
=={{header|CMake}}==
<
0 OFF no False n Princess-NOTFOUND)
if(var)
Line 750:
message(STATUS "${var} is false.")
endif()
endforeach(var)</
<pre>-- 1 is true.
Line 773:
===Booleans===
Booleans are defined as any data item having a <code>PICTURE</code> made up of ones.
<
The boolean literals <code>B"1"</code> and <code>B"0"</code> represent true and false, respectively.
Line 779:
===Conditions===
Prior to COBOL 2002, there was no boolean data type, only ''condition names'' which could be used in conditional expressions. Condition names are subordinate to another data item, have the level-number 88, and are defined with the value(s) which their parent data item must have for them to be set to true. They can be defined like so:
<
88 X-Is-One VALUE 1.
88 X-Is-Even VALUE 0 2 4 6 8.
88 X-Larger-Than-5 VALUE 6 THRU 9.</
Conditions can be <code>SET</code> to <code>TRUE</code> or <code>FALSE</code>. Setting a condition to <code>TRUE</code> will move the (first) value in the <code>VALUE</code> clause to the parent data item. In COBOL 2002, an optional <code>FALSE</code> clause was added which allowed the condition to be <code>SET</code> to <code>FALSE</code> and consequently set the parent data item to the specified value in the clause. A <code>FALSE</code> clause can only have one value. An example of conditions in action:
<
DATA DIVISION.
Line 812:
DISPLAY "Foo is zero."
END-IF
.</
{{out}}
Line 824:
CoffeeScript is largely based on JavaScript, but that may only serve to confuse you. Your best bet is to learn all the cases:
<
h1 = {foo: "bar"}
h2 = {foo: "bar"}
Line 853:
false and true
]
</syntaxhighlight>
=={{header|Common Lisp}}==
Line 863:
=={{header|Component Pascal}}==
<
VAR
b,c: BOOLEAN;
Line 870:
c := FALSE;
...
</syntaxhighlight>
=={{header|Crystal}}==
Line 877:
As mentioned in the [https://crystal-lang.org/reference/syntax_and_semantics/truthy_and_falsey_values.html language reference], the values <code>nil</code> and <code>false</code>, as well as null pointers are "falsey", all other values are truthy
<
puts "false"
elsif nil
Line 885:
elsif true && "any other value"
puts "finally true!"
end</
=={{header|D}}==
Line 912:
=={{header|Dylan}}==
<
#f // <boolean> false</
For the purpose of conditional statements, all objects other than <tt>#f</tt> evaluate to true.
Line 924:
E defines two basic objects <code>true</code> and <code>false</code>, and the <code>boolean</code> [http://wiki.erights.org/wiki/Guard guard] which accepts them. All builtin operations which take booleans (e.g. the <code>if</code> control structure) coerce the input to boolean.
<
# value: "a"
Line 931:
? if (90) { "a" } else { "b" }
# problem: the int 90 doesn't coerce to a boolean</
No objects in the standard library coerce to boolean, but user-written objects may choose to do so; they can then be used in place of booleans.
<
> to __conformTo(guard) {
> if (guard == boolean) { return true }
Line 941:
> }
> if (bowlian) { "a" } else { "b" }
# value: "a"</
=={{header|EchoLisp}}==
"All that which is not false is true" - Attribué à L. Wittgenstein - The only false value is the boolean #f. '''All''' other objects, including the empty list or null or 0 ..- evaluate to #t = true.
<
(not #t) → #f
(not #f) → #t
(not null) → #f
(not 0) → #f
</syntaxhighlight>
=={{header|EGL}}==
Line 956:
A boolean can be converted to a string ("true" or "false") using StrLib.booleanAsString(boolean);
<
myBool boolean = 0;
SysLib.writeStdout("myBool: " + StrLib.booleanAsString(myBool));
Line 977:
myInt = true;
SysLib.writeStdout("myInt: " + StrLib.booleanAsString(myInt));
</syntaxhighlight>
{{out}}
Line 999:
=={{header|Elixir}}==
Elixir utilizes Erlang's definition of boolean types; they're defined as the atoms <tt>:true</tt> and <tt>:false</tt>. No other type is equal to true or false.
<
iex(1)> true === :true
true
Line 1,006:
iex(3)> true === 1
false
</syntaxhighlight>
nil (also defined as an atom, <tt>:nil</tt>) is not equal to false.
<
iex(4)> nil === :nil
true
iex(5)> nil === false
false
</syntaxhighlight>
=={{header|Elm}}==
<
--True and False directly represent Boolean values in Elm
--For eg to show yes for true and no for false
Line 1,061:
display expr=
div [] [ text ( toString expr ++ "-->" ++ toString(evaluate expr) ) ]
--END</
=={{header|Emacs Lisp}}==
Line 1,071:
Erlang doesn't technically define boolean types. Instead, the atoms <tt>true</tt> and <tt>false</tt> are used. However, they are integrated well enough into the language there should be no problem with that as long as you don't expect false and true to mean anything but literal false and true.
<
true
2> 1 < 1.
false
3> 0 == false.
false</
=={{header|Excel}}==
The Logical category of functions includes the constants TRUE() and FALSE() which are displayed without the parantheses in cells. There are logical functions such as AND and OR too. For an AND truth table of two variables, take 3 cells, say A1,B1 and C1. In C1 type in :
<
Copy this until C4. Now as values are filled in from A1-A4 and B1-B4, C1-C4 gets updated.
<syntaxhighlight lang=text>0 0 FALSE
0 1 FALSE
1 0 FALSE
1 1 TRUE</
=={{header|F_Sharp|F#}}==
The type bool is an abbreviation for the .NET framework type <code>System.Boolean</code>.
<
Instances of this type have values of either <code>true</code> or <code>false</code>.
Line 1,107:
=={{header|Forth}}==
In conditionals, zero is false, non-zero is true. There are predefined constants for the canonical forms. For FORTH-83 or later, FALSE is zero and TRUE is -1 (all bits set). For earlier FORTH standards, FALSE is zero and TRUE is 1.
<
FALSE . \ 0</
=={{header|Fortran}}==
Line 1,115:
Fortran 66 introduced a '''logical''' data type which can be set to either '''.true.''' or '''.false.''' or be generated via logical expressions such as <=, etc. Such variables cannot be used in normal arithmetic with operators such as +-*/ but only with logical operators such as .OR. and so on. If via the EQUIVALENCE statement their numerical values (or, bit patterns) are inspected as say an integer, the values may well not be as anticipated and differ between computers and compilers. For instance, on the Burroughs 6700 an '''integer''' variable equivalenced to a '''logical''' variable would appear as '''.true.''' if odd, '''.false.''' if even.
The default storage size of a LOGICAL variable is the same as the default storage size of an INTEGER variable, which for many systems is 32 bits. This is done to simplify calculations of record sizes, or the alignment of variables in COMMON storage areas. It is usually possible to declare variables with certain byte sizes (normally only powers of two) so that LOGICAL*1 or similar declarations may be available. If used however there may arise alignment issues with adjacent variables of other types (such as REAL) that may require padding to even word boundaries for best access. Consider <
LOGICAL*1 LIVE
REAL*8 VALUE
END TYPE MIXED
TYPE(MIXED) STUFF(100)</
The array STUFF might occupy 900 bytes, or, 1600 bytes if each double-precision value has to be aligned to an eight-byte boundary. In the latter case, it may be better to declare LIVE and VALUE to be separate hundred-element arrays as in <
LOGICAL*1 LIVE(100)
REAL*8 VALUE(100)
END TYPE MIXED
TYPE(MIXED) STUFF</
Except that now only hundred-element variables of type MIXED can be declared. Either way, the record size needed for a disc file holding such items will need careful thought.
Line 1,130:
In addition to the types defined by [[#Object Pascal|Object Pascal]], free Pascal defines the <code>qWordBool</code>, that has a <code>sizeOf</code> eight.<br>
Furthermore, True and False are not keywords from FPC v3.0.0. It is possible to assign any value to true and false, like strings but even objects.<br>
<
const
true = 'true';
Line 1,154:
END;
</syntaxhighlight>
JPD 2022/08/02
Line 1,174:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=65324112fde86d51937b9cfcca0c51f9 Click this link to run this code]'''
<
Dim bX As Boolean
Line 1,181:
Print bX
End</
Output:
<pre>
Line 1,189:
=={{header|GAP}}==
<
# true
Line 1,201:
fail = fail;
# true</
=={{header|Go}}==
Line 1,208:
The template package however, uses a different rule for <tt>if</tt> actions. There, it is testing if a "pipeline" is "empty" where the empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.
<
package main
Line 1,234:
bolStr, _ := strconv.ParseBool(str1)
fmt.Println("After :", reflect.TypeOf(bolStr)) // prt After : bool
}</
=={{header|Groovy}}==
Line 1,245:
The Haskell standard [http://haskell.org/haskellwiki/Prelude Prelude] defines a data type <code>Bool</code>, which has exactly two members:
<
In addition to all the functionality of any other Haskell algebraic data type (e.g. [[pattern matching]]), and the specified derived typeclass instances (e.g. <code>False == False</code>, <code>succ False == True</code>, <code>(maxBound :: Bool) == True</code>, etc.), the built-in guard (“<code>|</code>”) and <code>if</code> syntaxes use Bool.
Line 1,265:
=={{header|i}}==
Any non-zero number is true in 'i'.
<
//Bits aka Booleans.
b $= bit()
Line 1,285:
b $= 0
print(b)
}</
=={{header|Icon}} and {{header|Unicon}}==
Line 1,291:
=={{header|Idris}}==
<
Data type Prelude.Bool.Bool : Type
Boolean Data Type
Line 1,300:
True : Bool
</syntaxhighlight>
=={{header|Inform 6}}==
Line 1,309:
However, Inform 7 distinguishes between Boolean values and conditions. Comparison expressions do not return truth states, and truth state expressions cannot be used directly in conditional statements. There is a conversion from condition to truth state:
<
And truth states can be used in conditions by adding an explicit comparison:
<
Phrases (functions) cannot be defined to return a truth state directly. Instead, they are defined using "to decide whether" (or "to decide if") and can then be used as conditions:
<
if 123 is greater than 100, decide yes;
otherwise decide no.
Line 1,322:
let B be whether or not the CPU is working correctly;
[...or use as a condition]
if the CPU is working correctly, say "Whew."</
=={{header|J}}==
Line 1,364:
Other objects do not represent boolean values and cannot be used in conditional expressions, for example:
<
println("true")
end
ERROR: type: non-boolean (Int64) used in boolean context</
However, integers can be converted to boolean types with the <code>bool()</code> function (which treats nonzero values as <code>true</code>)
<
5-element Bool Array:
true
Line 1,375:
false
true
true</
=={{header|KonsolScript}}==
Line 1,393:
=={{header|Lambdatalk}}==
Predefined constants are true and false:
<
{if true then YES else NO}
-> YES
{if false then YES else NO}
-> NO
</syntaxhighlight>
Following the lambda calculus user defined booleans can be built
<
{def TRUE {lambda {:a :b} :a}}
-> TRUE
Line 1,413:
-> no
</syntaxhighlight>
=={{header|Lasso}}==
Line 1,420:
A variable can also be assigned a boolean type, and as such then holds either true of false states.
<
// => false
Line 1,430:
$x = false
$x // => false</
In a conditional, if the result is the integer 0, it is also evaluated as boolean false.
If the conditional results in an integer greater than zero, it is evaluated as boolean true.
<
// size is 0
#x->size ? 'yes' | 'no'
Line 1,441:
local(x = '123fsfsd')
// size is 8
#x->size ? 'yes' | 'no'</
{{out}}
Line 1,454:
=={{header|LFE}}==
<
> 'true
true
Line 1,463:
> (or 'false 'true)
true
</syntaxhighlight>
=={{header|Lingo}}==
Lingo has the constants TRUE and FALSE. In numerical context they have the values 1 and 0. In boolean context any nonzero integer evaluates to TRUE.
<
-- 1
put FALSE
-- 0
if 23 then put "Hello"
-- "Hello"</
=={{header|Little}}==
Line 1,486:
<
int b = 1;
int c;
Line 1,497:
if (!defined(c)) {puts("fourth test is true");} // This should print
if (str1) {puts("fifth test str1 is true");} // This should print
if (str2) {puts("sixth test str2 is false");} // This should not print</
=={{header|LiveCode}}==
Line 1,504:
=={{header|Logo}}==
Logo has predefined symbols for true and false (<code>"true</code> and <code>"false</code>), which are the values returned by predicates and required by logical operators and conditionals.
<
print 1 > 0 ; true
if "true [print "yes] ; yes
if not "false [print "no] ; no</
Unlike other lispy languages, there are no other implicit conversions.
You must test explicitly for zero or empty collections.
<
if empty? [] [print "empty] ; empty list
if empty? "|| [print "empty] ; empty word</
=={{header|Lua}}==
All values in Lua other than <code>false</code> or <code>nil</code> are considered <code>true</code>:
<
if "" then print"empty string" end -- This prints
if {} then print"empty table" end -- This prints
if nil then print"this won't print" end
if true then print"true" end
if false then print"false" end -- This does not print</
=={{header|M2000 Interpreter}}==
Line 1,537:
<
Module CheckBoolean {
A=True
Line 1,568:
Print str$(2, "\t\r\u\e;\t\r\u\e;\f\a\l\s\e")="true"
</syntaxhighlight>
=={{header|Maple}}==
Line 1,588:
Sample Usage: (islogical() is a function that returns a boolean "1" if the input is a boolean, "0" otherwise)
<
ans =
Line 1,622:
ans =
0</
=={{header|Maxima}}==
<
/* true */
Line 1,635:
not false;
/* true */</
=={{header|Metafont}}==
Line 1,655:
In MiniScript, numbers represent boolean values, with additional fuzzy logic for degrees of truth. Built-in constants `true` and `false` are simply aliases for 1 and 0, respectively.
<
boolFalse = false
Line 1,665:
kindaTrue = 0.4
print "mostlyTrue AND kindaTrue: " + (mostlyTrue and kindaTrue)
print "mostlyTrue OR kindaTrue: " + (mostlyTrue or kindaTrue)</
{{out}}
<pre>boolTrue is true, and its value is: 1
Line 1,673:
=={{header|Mirah}}==
<
import java.util.HashMap
Line 1,702:
#puts 'FALSE is false' if !FALSE # ==> FALSE does not exist
</syntaxhighlight>
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 1,721:
done := A > B
UNTIL done
END boo.</
=={{header|Modula-3}}==
Similar to [[Ada]], Modula-3 has a built-in <tt>BOOLEAN</tt> type defined as
<
=={{header|Monte}}==
Line 1,731:
Much like [[E]], Monte has built-in objects <tt>true</tt> and <tt>false</tt>, and a boolean [http://wiki.erights.org/wiki/Guard guard].
<
def example(input :boolean):
if input:
return "Input was true!"
return "Input was false."
</syntaxhighlight>
=={{header|MUMPS}}==
Line 1,759:
=={{header|Nanoquery}}==
<
b = false
Line 1,766:
else if b
println "b is true"
end</
{{out}}
<pre>a is true</pre>
Line 1,777:
is false, 0 or null. $istrue returning true if value is not false, not 0 and not null.
<
$print(true, "\n");
$print(false, "\n");
Line 1,803:
} else {
$print("$istrue(1) tests false\n");
}</
{{out}}
Line 1,823:
Defining <code>true</code> and <code>false</code> variables can lead to name collisions during compilation so a simple expedient is to define boolean functions <code>isTrue</code>
and <code>isFalse</code> to return the appropriate values.
<
options replace format comments java crossref savelog symbols nobinary
Line 1,841:
method isFalse public static returns boolean
return \isTrue
</syntaxhighlight>
{{out}}
Line 1,856:
=={{header|Nim}}==
<
if false: echo "no"
# Other objects never represent true or false:
if 2: echo "compile error"</
=={{header|Oberon-2}}==
<
VAR
a,b,c: BOOLEAN;
Line 1,870:
b := FALSE;
c := 1 > 2;
</syntaxhighlight>
=={{header|Objeck}}==
Line 1,891:
OCaml defines a built-in data type <code>bool</code>, which has exactly two members, represented by the keywords <code>true</code> and <code>false</code>:
<
In addition to all the functionality of any other OCaml algebraic data type (e.g. [[pattern matching]]), and the functionality of any other OCaml data type (e.g. comparisons <code>false = false</code>, <code>false < true</code>, etc.), <code>bool</code> is also used in the guards in pattern matching (“<code>when</code>”) and <code>if</code> and <code>while</code> syntaxes.
Line 1,946:
=={{header|Perl}}==
<
my $true_or_false = $x ? 'true' : 'false'; # false</
or
<
my $true_or_false;
Line 1,958:
else {
$true_or_false = 'false';
}</
The values in Perl that are false are: <tt>0</tt> (as a number (including <tt>0.0</tt>), or as the string <tt>'0'</tt>, but '''not''' the string <tt>'0.0'</tt>), the empty string <tt><nowiki>''</nowiki></tt>, the empty list <tt>()</tt>, and <tt>undef</tt>. Everything else is true. See [http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood perlsyn].
Line 1,965:
Boolean comparison of zero against itself gives a value of one, but Perl uses short circuit evaluations, so any true or false value may be returned from a boolean expression:
<
print (2 && 7); # 7, rather than 1(true)
print (7 xor 2); # empty string, rather than 0(false)
print ('apples' && 'pears'); # pears, rather than 1(true)
print ('apples' xor 'pears'); # empty string, rather than 0(false)</
=== Objects ===
Line 1,979:
Perl has no builtin "true" or "false" keywords. This is a caveat, because true and false are bareword strings and evaluate to true:
<
# true and false are not special so will be treated as bareword strings
if (true) { print "true is true\n" }; # This prints
if (false) { print "false is true\n" }; # So does this
if (spongebob) { print "spongebob is true\n" }; # A bareword string</
=== Special cases ===
Line 2,000:
The following example illustrates, and also emphasies the subtlety of the issue (no difference whatsoever if c, d, e, f are defined as bool):
<!--<
<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>
Line 2,019:
-- 3==2:false(0) ==1:false, eq1:false, ==true:false
--</span>
<!--</
=={{header|PHP}}==
Line 2,030:
Picat has the built-in <code>true/0</code> for true (it always succeeds) and <code>false/0</code> (or <code>fail/0</code>) for false. <code>false/0</code> (/<code>fail/0</code>) can be used to generate other solutions through backtracking.
<
member(N,1..5),
println(N),
fail, % or false/0 to get other solutions
nl.
go => true.</
{{out}}
Line 2,062:
=={{header|Pike}}==
<
> 0;
(3) Result: 0
Line 2,076:
(9) Result: 1
>
</syntaxhighlight>
=={{header|PL/I}}==
True is <code>'1'b</code> and false is <code>'0'b</code>.
<
x='1'b; /* True */
x='0'b; /* False */</
Using the macro facility one can define reasonable symbols for true and false
<
tf: proc options(main);
%Dcl true char; %true='''1''b';
Line 2,094:
Else
Put Skip List('false was recognized');
End;</
{{out}}
<pre>That's true
Line 2,103:
In PL/M, even numbers are falsy and odd numbers are truthy. That is to say, conditional expressions test only the low bit of the value.
<
IF 1 THEN /* THIS WILL */;
IF 2 THEN /* THIS WON'T */;
IF 3 THEN /* THIS WILL */;</
Canonically, false is represented by <code>0</code> (all bits clear), and true by <code>0FFH</code> (all bits set). These are the values that conditional operators (like <code>=</code>) return.
<
A = 4 < 5;
/* A IS NOW 0FFH */</
Boolean literals are not included by default, but it is not uncommon for programmers to define them by hand:
<
=={{header|Plain English}}==
Line 2,127:
Predefined constants are:
<
false</
=={{header|PowerShell}}==
Two automatic variables exist for this purpose:
<
$false</
However, nearly everything can be converted to a boolean value, as detailed in the following list:
* any non-zero number evaluates to '''true''', zero evaluates to '''false'''
Line 2,154:
'''Some examples:'''
<
True
>>> not True
Line 2,192:
False
>>> bool("False")
True</
=={{header|Quackery}}==
Line 2,207:
Racket has the standard Scheme Boolean values <tt>#t</tt> and <tt>#f</tt>, and will also accept <tt>#true</tt> and <tt>#false</tt>. This is a literal syntax, so it can be used anywhere including in quoted positions. There are also bindings for <tt>true</tt> and <tt>false</tt> (but of course when these are quoted, the result is plain symbols). Like other Scheme descendants, many conditional constructs treat any non-false value as "truthy." So, for instance,
<
['bloggle 'pear]
[else 'nectarine])</
... evaluates to <tt>'pear</tt>, because <tt>'bloggle</tt> is not false.
Line 2,219:
Raku provides an enumeration <code>Bool</code> with two values, <code>True</code> and <code>False</code>. Values of enumerations can be used as ordinary values or as mixins:
<syntaxhighlight lang=raku
my $val = 0 but True;</
For a discussion of Boolean context (how Raku decides whether something is True or False): https://docs.raku.org/language/contexts#index-entry-Boolean_context.
Line 2,226:
=={{header|Raven}}==
Raven considers 0 as <code>FALSE</code>, -1 as <code>TRUE</code>
<
FALSE print
2 1 > print # TRUE (-1)
3 2 < print # FALSE (0)
42 FALSE != # TRUE (-1)</
=={{header|REBOL}}==
Line 2,277:
===simplistic===
<
false = 0</
===spruced up===
Some programmers like to "spruce up" such a simple assignment:
<
false = (1=0)</
===more exactitudeness===
<
false = (1==0)</
===oblique===
<
false = \true</
[The parentheses aren't necessary in all of the above versions.]
<br><br>Some REXX interpreters allow the ''NOT'' (<code>¬</code>) character for negation:
<
===esoteric===
<
false = 'war' = 'peace'
false = 'freedom' = 'slavery'
false = 'ignorance' = 'strength'</
Of course, in Orwellian terms, the above '''false''' statements are '''true''', but REXX isn't an Eric Arthur Blair reader.
<br><br>
=={{header|Ring}}==
<
x = True
y = False
Line 2,311:
see "x or y : " + (x or y) + nl
see "not x : " + (not x) + nl
</syntaxhighlight>
=={{header|Ruby}}==
Line 2,322:
=={{header|Rust}}==
<
fn main() {
// Rust contains a single boolean type: `bool`, represented by the keywords `true` and `false`.
Line 2,338:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,348:
The BOOL type can be <code>true</code> or <code>false</code>. Sather never implicitly does casting of a type in another, so numeric value or other types cannot be used (implicitly) as boolean value; nonetheless an explicit "cast" can be done:
<
i:INT := 1;
v := 1; -- wrong
Line 2,354:
-- BUT
v := 1.bool; -- ok
if i.bool then ... end; -- ok</
In this case, <code>0.bool</code> is false, and <code>n.bool</code> with n not equal to 0 is true.
Line 2,375:
=={{header|SenseTalk}}==
True, Yes, and On are true; False, No, Off and Empty (an empty string) are false.
<
put it & " is " & (it is true)
end repeat
</syntaxhighlight>
{{out}}
<pre>
Line 2,392:
=={{header|Sidef}}==
Sidef defines the ''true'' and ''false'' boolean values, which are part of the ''Bool'' type.
<
var f = false;</
In conditional expressions, anything that evaluates to zero or nothing is considered ''false'', including empty arrays and empty hashes.
<
say "true"
} else {
say "false";
}</
{{out}}
<pre>false</pre>
Line 2,416:
=={{header|SNUSP}}==
Zero is false and non-zero is true, as used by the sole skip-if-zero operator ('''?''').
<
\-/</
=={{header|SPL}}==
Line 2,426:
Standard ML defines a top-level data type <code>bool</code>, which has exactly two members, <code>true</code> and <code>false</code>:
<
In addition to all the functionality of any other Standard ML algebraic data type (e.g. [[pattern matching]], equality <code>false = false</code>), <code>bool</code> is also used in <code>if</code> and <code>while</code> syntaxes.
Line 2,445:
Any of these values may be abbreviated, and mixed-case spellings are also acceptable. [http://www.tcl.tk/man/tcl8.5/TclLib/GetInt.htm]
Any other value gives an error. In an interactive tclsh session:
<
expected boolean value but got ""</
Test for the boolean value of a string can be stuff like
<
which will test for "no" or "NO" or "0" or "False" or ...
Line 2,470:
In the following example, after running the test command, the then syntactical component runs the optional branch if an exitcode is of zero determined:
<
echo 'Looking for file' # This is the evaluation block
test -e foobar.fil # The exit code from this statement determines whether the branch runs
Line 2,477:
echo 'I am going to delete it'
rm foobar.fil
fi</
In some later shells, the values ''true'' and ''false'' are defined, respectively, as a return code of 0 and a return code of greater-than zero. While there are built-in functions for each of these values, booleans are most commonly the result of a test or a process termination.
Line 2,484:
{{works with|ksh}}
<
=={{header|Ursa}}==
Ursa has the boolean data type which can be declared using the declare (or decl) function.
<syntaxhighlight lang
Boolean values can be set to either true or false, or the result of an expression.
<
# same as
set bool (= 2 2)
</syntaxhighlight>
or
<
# same as
set bool (not (= 2 2))
</syntaxhighlight>
=={{header|VBA}}==
VBA has a boolean type. As an integer False is 0 and anything else is True. However True converts to -1. Booleans are False by default.
<
Dim b As Boolean
Debug.Print b
Line 2,510:
Debug.Print b
a = b
Debug.Print a</
{{out}}
Output of above lines:<pre>
Line 2,521:
VBScript has the boolean subdatatype and also the two constants <code>True</code> and <code>False</code>.
When converting an integer to a boolean, <code>0</code> is <code>False</code> and anything not equal to <code>0</code> is <code>True</code>.
<syntaxhighlight lang=vb>
a = True
b = False
Line 2,527:
x = Int(Rnd * 2) <> 0
y = Int(Rnd * 2) = 0
MsgBox a & " " & b & " " & x & " " & y</
{{out}}
<pre>
Line 2,537:
Since a <code>String</code> is converted automatically to a <code>Number</code> when necessary, the following will print "false" because "foo" is converted to 0:
<
echo "true"
else
echo "false"
endif</
=={{header|Vlang}}==
V has a ''bool'' type, with literal values for ''true'' and ''false''. Numeric values are not used in conditional statements. 0 is not treated as false, and non-zero does not mean true, in V.
<
// Tectonics: v run boolean-value.v
module main
Line 2,563:
if 0 == 1 { println("bad result") } else { println(f) }
}</
{{out}}
Line 2,574:
WDTE has a built-in boolean type, the two values of which are exposed by the <code>std</code> package's <code>true</code> and <code>false</code> functions. In general, however, built-in conditional functionality, such as <code>switch</code> expressions, considers any value that is not <code>true</code> to be <code>false</code>.
<
let ex => switch 'this is irrelevant for this example' {
false => 'This is, obviously, not returned.';
Line 2,580:
true => 'This is returned.';
};
ex -- io.writeln io.stdout;</
The above prints "This is returned."
Line 2,588:
This class has two methods: the operator ''!'' which returns the logical complement of its receiver and ''toString'' which returns its string representation.
<
System.printAll([embed, ", ", !embed, ", ", "Is Wren embeddable? " + embed.toString])</
{{out}}
Line 2,605:
=={{header|XSLT}}==
<
True and false are returned by built-in XPath functions.
</xsl:if>
Line 2,613:
<xsl:if test="@myAttribute or not($nodeSet)">
Test an attribute for its presence (empty or not), or whether a node set is empty.
</xsl:if></
=={{header|Z80 Assembly}}==
Line 2,619:
All of the following can be used to test if the rightmost bit of a byte is 1.
<
jr nz,true
;;;;;;;;;;;;;;;;
Line 2,626:
;;;;;;;;;;;;;;;;
RRCA
jr c,true</
Of these three methods, the last one is the fastest and takes the least amount of bytes to encode. However, it does destroy the variable, which may not be an option depending on what you need to do with that data later. <code>BIT</code> is slower than the rest but doesn't alter registers or memory, just the flags, which is sometimes exactly what you need.
Line 2,633:
=={{header|zkl}}==
<
b:=False;
True.dir();</
{{out}}
<pre>
Line 2,649:
=={{header|zonnon}}==
<
var
Line 2,658:
c := 1 > 2;
...
</syntaxhighlight>
{{omit from|ML/I}}
|