Boolean values: Difference between revisions

m
m (Automated syntax highlighting fixup (second round - minor fixes))
imported>Tromp
 
(31 intermediate revisions by 18 users not shown)
Line 363:
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.
 
<syntaxhighlight lang="gwbasicbasic">10 LET A%=0
20 LET B%=NOT(A%)
30 PRINT "THIS VERSION OF BASIC USES"
Line 377:
==={{header|Applesoft BASIC}}===
IF statement condition treats any non-zero numeric value as true. Comparison operators evaluate to 1 (true) or 0 (false).
Examples:<syntaxhighlight lang="applesoftbasicbasic">? 2 = 3
? 2 = 2
IF 7 THEN ?"HELLO"</syntaxhighlight>
Line 387:
 
==={{header|BaCon}}===
<syntaxhighlight lang="qbasicbasic">' Boolean TRUE and FALSE are non-zero and zero constants
a = TRUE
b = FALSE
Line 410:
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256basic">
' BASIC256 used numbers to represent true and false
' values. Zero is false and anything else is true.
Line 421:
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasicbasic"> REM BBC BASIC uses integers to represent Booleans; the keywords
REM FALSE and TRUE equate to 0 and -1 (&FFFFFFFF) respectively:
PRINT FALSE
PRINT TRUE</syntaxhighlight>
 
==={{header|Cherrycake}}===
The boolean value of true in Cherrycake is represented with a 'true' keyword, and the boolean value of false in Cherrycake is represented with a 'false' keyword. Booleans can also be represented with other types, such as binary and integers. In binary types, 0x01 and 0b01 represent true, and 0x00 and 0b00 represent false. In integer types, 1 represents true and 0 represents false.
 
==={{header|Commodore BASIC}}===
Commodore BASIC evaluates any non-zero number for TRUE&mdash;but is typically represented as 16-bit signed integer value of -1 or $FFFF&mdash;and zero evaluates to FALSE.
 
<syntaxhighlight lang="gwbasicbasic">10 f%=("z"="a") : t%=not f% : rem capture a boolean evaluation
15 print chr$(147);chr$(14);
20 print "True is evaulated as:";t%
Line 473 ⟶ 476:
 
Sample code:
<syntaxhighlight lang="freebasicbasic">' FB 1.05.0 Win64
 
Dim i As Integer = 23
Line 502 ⟶ 505:
==={{header|Microsoft Small Basic}}===
Microsoft Small Basic has two constants: <code>"True"</code> and <code>"False"</code>.<br>
<syntaxhighlight lang="smallbasicbasic">If c Then
notc = "False"
Else
Line 512 ⟶ 515:
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]].)
 
<syntaxhighlight lang="powerbasicbasic">DIM x AS LONG
x = ISTRUE(1 = 1) ' returns -1
x = ISTRUE(1 = 0) ' returns 0
Line 524 ⟶ 527:
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:
 
<syntaxhighlight lang="qbasicbasic">
CONST FALSE=0
CONST TRUE = Not FALSE
Print FALSE
Print TRUE
</syntaxhighlight>
</Lang>
 
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:
 
<syntaxhighlight lang="qbasicbasic">
10 DEF FNFALSE = 0
20 DEF FNTRUE = NOT FNFALSE
30 Print FNFALSE
40 Print NFTRUE
</syntaxhighlight>
</Lang>
 
==={{header|Run BASIC}}===
Basically 0 is false and 1 is true
<syntaxhighlight lang="runbasicbasic">if 1 then print "1 is true"
if not(0) then print "0 is false"
if 1 < 2 then print "1 < 2 TRUE"
Line 548 ⟶ 551:
if not(2 < 1) then print "2 not < 1"
if not(1 = 0) then print "1 not = 0"</syntaxhighlight>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="basic">
a = true
b = false
</syntaxhighlight>
 
==={{header|smart BASIC}}===
Line 587 ⟶ 596:
==={{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="textbasic">t = 1 = 1
f = 0 = 1
 
Line 597 ⟶ 606:
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasicbasic">
// Yabasic usa números para representar los valores true y false
// Las constantes incorporadas true y false representan uno y cero respectivamente.
Line 613 ⟶ 622:
<br>When converting an integer to a boolean, <code>0</code> is <code>False</code> and anything not equal to <code>0</code> is
 
<syntaxhighlight lang="vbbasic">Dim x As Boolean
x = IIf(Int(Rnd * 2), True, False)
MsgBox x</syntaxhighlight>
 
=={{header|Batch File}}==
The closest thing to Boolean values in batch files is using <code>if defined</code>.
Line 655 ⟶ 665:
=={{header|Befunge}}==
Zero is false, non-zero is true. This is only used by the horizontal and vertical switch operators (<code>_</code> and <code>|</code>).
 
=={{header|Binary Lambda Calculus}}==
 
The standard representations for the booleans in lambda calculus are true = \then. \else. then, false = \then. \else. else, which correspond to BLC programs <code>00 00 110</code> and <code>00 00 10</code>.
 
=={{header|BQN}}==
 
Line 897 ⟶ 912:
> if (bowlian) { "a" } else { "b" }
# value: "a"</syntaxhighlight>
 
=={{header|EasyLang}}==
EasyLang does not have built-in boolean types or constants. Operators that "return" a boolean type (e.g. >, <=) cannot be used outside of conditional statements and loops.
 
You can use 1 or 0 in place of true and false.
 
<syntaxhighlight lang="easylang">
boolNumber = 1
if boolNumber = 1
print "True"
else
print "False"
.
</syntaxhighlight>
 
=={{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.
Line 905 ⟶ 935:
(not 0) → #f
</syntaxhighlight>
 
=={{header|Ecstasy}}==
Ecstasy's defines [https://github.com/xtclang/xvm/blob/master/lib_ecstasy/src/main/x/ecstasy/Boolean.x an enumeration named Boolean], which contains the values <span style="background-color: #e5e4e2"><tt>&nbsp;False&nbsp;</tt></span> and <span style="background-color: #e5e4e2"><tt>&nbsp;True&nbsp;</tt></span>.
<syntaxhighlight lang="java">
module GeorgeBoole {
@Inject Console console;
 
void run() {
Boolean f = False;
assert !f == True;
 
// methods like "and", "or", "xor", and "not" are the same as
// the operators "&"/"&&", "|"/"||", "^"/"^^", and the unary "~"
assert True.and(False) == True & False == False;
assert True.or(False) == True | False == True;
assert True.xor(False) == True ^ False == True;
assert True.not() == ~True == False;
 
console.print($"0==1 = {0==1}");
console.print($"!False = {!False}");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
0==1 = False
!False = True
</pre>
 
=={{header|EGL}}==
In EGL boolean is a primitive type, however it acts the same as an integer (type int). A boolean and an int accept integer values aswel as true and false keywords (which represent resp. 1 and 0). A boolean is always true except when it has value 0 (or keyword false).
Line 1,016 ⟶ 1,076:
 
In an <code>if</code> and similar, <code>nil</code> is false and anything else is true. To make that clear docstrings etc say "non-nil" for true. (See last item in [http://www.gnu.org/software/emacs/manual/html_node/elisp/Documentation-Tips.html elisp manual documentation tips].)
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal has a dedicated Logical type expressed by the logic keyword.
|It's not nullable and holds the two values false and true.
|There are no implicit conversions, but explicit conversions
|from/to int (0,1) or text ("⊥", "⊤") are allowed.
|^
logic booleanTrue = true
logic booleanFalse = false
if 2 > 1 and true and not false
writeLine("true: " + true + ", false: " + false)
end
if false == logic!0
writeLine("explicit conversion from integer")
end
if true == logic!"⊤"
writeLine("explicit conversion from text")
end
writeLine(int!true) # is one
writeLine(text!false) # is "⊥"
</syntaxhighlight>
{{out}}
<pre>
true: ⊤, false: ⊥
explicit conversion from integer
explicit conversion from text
1
</pre>
 
=={{header|Erlang}}==
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.
Line 1,109 ⟶ 1,200:
 
Futhark has a <code>bool</code> type, with the two values <code>True</code> and <code>False</code>. They are used for branching.
 
 
=={{header|FutureBasic}}==
FB recognizes two types for boolean truth values: BOOL and boolean. There is a subtle difference between the two. A BOOL will accept without complaint the macros YES, and NO, the equivalent native FB constants, _true, and _false, and, of course, 0 and 1. However, although a BOOL can be assigned other values, it will throw a clang (FB"s native compiler) warning as with this example:
<syntaxhighlight lang="futurebasic">
window 1
BOOL boolTest
boolTest = -1
print boolTest
HandleEvents
</syntaxhighlight>
When compiled this code generates this warning:
<syntaxhighlight lang="futurebasic">
implicit conversion from constant value -1 to 'BOOL'; the only well defined values for 'BOOL' are YES and NO [-Wobjc-bool-constant-conversion]
</syntaxhighlight>
On the other hand, a Boolean can be assigned not only YES, NO, _true, or _false, and 0 or 1, but also other values, such as the common -1, and compile without complaint.
 
Since FB can also work with objects, native BOOLs and booleans need to be converted to objects as demonstrated in the code below.
 
Trivia: Because NULL and _nil zero values in FB, they evaluate to "NO" or “_false” in conditional expressions.
<syntaxhighlight lang="futurebasic">
void local fn BooleanExercise
BOOL areEqual = (1 == 1) // areEqual is YES
BOOL areNotEqual = not areEqual /* areNotEqual is converted to: areEqual = (-(1 == 1)). -1 throws a clang warning.
NOTE: FB does not accept the "!" shorthand for "not", i.e. !areEqual, common in other languages. */
print "areEqual == "; areEqual
print "areNotEqual == "; areNotEqual
print
// Boolean types assigned values outside YES or NO compile without complaint.
boolean minusOneTest = -1
print "minusOneTest == "; minusOneTest
// Typical boolean value is use
BOOL flatterRosettaReader = YES
if (flatterRosettaReader)
print
print @"Rosetta Code programmers understand booleans."
print
end if
// Defined Core Foundation boolean values
print "kCFBooleanTrue == "; kCFBooleanTrue
print "kCFBooleanFalse == "; kCFBooleanFalse
print
// Number object assigned literal value
CFNumberRef booleanObject = @(YES)
print "booleanObject == "; booleanObject
print
// Number object created programmatically
booleanObject = NO
print "booleanObject variable reassigned as N0 == "; fn NumberWithBool( booleanObject )
print
end fn
 
window 1
 
fn BooleanExercise
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
areEqual == 1
areNotEqual == 0
 
minusOneTest == -1
 
Rosetta Code programmers understand booleans.
 
kCFBooleanTrue == 1
kCFBooleanFalse == 0
 
booleanObject == 1
 
booleanObject variable reassigned as N0 == 0
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=65324112fde86d51937b9cfcca0c51f9 Click this link to run this code]'''
Line 1,249 ⟶ 1,421:
[...or use as a condition]
if the CPU is working correctly, say "Whew."</syntaxhighlight>
 
=={{Header|Insitux}}==
 
Simply <code>true</code> and <code>false</code>, however, anything which is not <code>false</code> or <code>null</code> is considered truthy.
 
=={{header|J}}==
 
Line 1,255 ⟶ 1,432:
This approach also works well with [[wp:Bayes'_theorem|Bayes' theorem]], as false matches 0% probability and true matches 100% probability.
=={{header|Java}}==
<p>
Java has <tt>true</tt> and <tt>false</tt> keywords, representing the only values of type <tt>boolean</tt>. There are also object wrappers <tt>Boolean.TRUE</tt> and <tt>Boolean.FALSE</tt>, of type <tt>Boolean</tt> which may be un-boxed into <tt>boolean</tt>s (auto-unboxed in Java 1.5+). There are no automatic conversions from any other types into <tt>boolean</tt>, and it is a compile-time error to use any type other than <tt>boolean</tt> or <tt>Boolean</tt> in a place that expects a <tt>boolean</tt> (e.g. if-statement condition, while-statement condition, operand of a logical operator, etc.).
In Java, <code>true</code> and <code>false</code> are used to reference a <code>boolean</code> value.<br />
There is no use of <kbd>0</kbd> and <kbd>1</kbd>, or <kbd>undefined</kbd> vs. <kbd>defined</kbd>.
</p>
<p>
As with the other primitive data-types, <code>boolean</code> has a wrapper class, <code>Boolean</code>, which includes a
set of valuable <kbd>boolean</kbd> operations.
</p>
<syntaxhighlight lang="java">
boolean valueA = true;
boolean valueB = false;
</syntaxhighlight>
<p>
Or.
</p>
<syntaxhighlight lang="java">
Boolean valueA = Boolean.TRUE;
Boolean valueB = Boolean.FALSE;
</syntaxhighlight>
<p>
Additionally.
</p>
<syntaxhighlight lang="java">
Boolean valueA = Boolean.valueOf(true);
Boolean valueB = Boolean.valueOf(false);
</syntaxhighlight>
 
=={{header|JavaScript}}==
The Boolean type has two values: <code>true</code> and <code>false</code>
Line 1,267 ⟶ 1,470:
 
(source: [http://www.ecma-international.org/publications/standards/Ecma-262.htm ECMAScript Language Reference])
 
=={{header|Joy}}==
The truth literals are <code>true</code> and <code>false</code>. In a conditional context, <code>false</code>, numerical values (including characters) of zero, empty lists and empty sets are evaluated as false.
 
=={{header|jq}}==
<tt>true</tt> and <tt>false</tt> are the only entities of type "boolean":
Line 1,281 ⟶ 1,488:
jq's logical operators, however, do not require boolean inputs. In brief, <tt>false</tt> and <tt>null</tt> are both regarded as false, and all other JSON entities are regarded as <tt>true</tt>. That is, all values except for <tt>false</tt> and <tt>null</tt> are truthy.
 
=={{header|Julia}}==
Julia has a built-in <code>Bool</code> type with values <code>true</code> and <code>false</code>.
Line 1,853 ⟶ 2,061:
=== There are no keywords for true and false ===
 
Perl has no builtin "true" or "false" keywords. ThisAn isold atrick caveat,of becauseusing truethem and false areas bareword strings andis strongly discouraged evaluatein tomodern true:Perl.
 
<syntaxhighlight lang="perl"># This does not work
# 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</syntaxhighlight>
 
=== Special cases ===
Line 2,165 ⟶ 2,367:
see "not x : " + (not x) + nl
</syntaxhighlight>
=={{header|Rockstar}}==
There are several synonyms for true and false. These include yes and no, or right and wrong. Therefore, to make a variable called Alice true and a variable called Bob false, you can write this.
<syntaxhighlight lang="rockstar">Alice was right.
Bob was wrong.</syntaxhighlight>
 
=={{header|RPL}}==
There is no boolean variable type in RPL. Boolean values are real numbers, zero meaning 'FALSE' and any other value meaning 'TRUE'; built-in boolean operators and functions always return 1 for TRUE.
RPL users have also access to boolean registers named flags, identified by a number, that can be cleared, set or tested.
{{works with|Halcyon Calc|4.2.7}}
1 == 1
1 == 2
≪ IF 42 THEN "TRUE" ELSE "FALSE" END ≫ EVAL
14 CF 14 FS?
14 SF 14 FS?
{{out}}
<pre>
5: 1
4: 0
3: "TRUE"
2: 0
1: 1
</pre>
 
=={{header|Ruby}}==
The only values in Ruby that are false are: <code>false</code> and <code>nil</code>. They have synonyms <code>FALSE</code> and <code>NIL</code>.
Line 2,172 ⟶ 2,397:
<code>false</code>, <code>nil</code> and <code>true</code> are singleton instances of classes <code>FalseClass</code>, <code>NilClass</code> and <code>TrueClass</code> respectively.
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UF]
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
Line 2,205 ⟶ 2,431:
v := 1.bool; -- ok
if i.bool then ... end; -- ok</syntaxhighlight>
 
In this case, <code>0.bool</code> is false, and <code>n.bool</code> with n not equal to 0 is true.
 
=={{header|S-BASIC}}==
Although S-BASIC has no explicit boolean data type, it allows integer,
real, fixed, string, and character variables to hold the results of boolean operations and to represent true and false conditions in IF, WHILE, and REPEAT statements. For types real.double, real, and fixed, a value of zero is false, and any non-zero value is true. For integers, the value 0 is treated as false, while -1 (or FFFFH), the bit-wise negation of zero, is treated as true.
For characters and strings, 'Y', 'y', 'T', and 't' are treated as true,
while 'N', 'n', 'F', and 'f' are treated as false. (For strings, only the first character is considered.) For convenience, the $CONSTANT compiler directive can be used to provide values for "true" and "false".
<syntaxhighlight lang = "BASIC">
$constant true = 0FFFFH
$constant false = 0
 
var a, another = char
var b, adult, age = integer
var c = real
 
repeat
begin
input "Applicant's age in years"; age
adult = (age >= 18)
if adult then
print "Applicant has full access"
else
print "Applicant has restricted access"
input "Do another (y/n)"; another
end
until not another
 
a = (2 > 3)
b = (2 > 3)
c = (2 > 3)
print "2 > 3 as char: "; a
print "2 > 3 as int : "; b
print "not (2 > 3) : "; not b
print "2 > 3 as real: "; c
print "not (2 > 3) : "; not c
end
</syntaxhighlight>
{{out}}
<pre>
Applicant's age in years? 19
Applicant has full access
Do another (y/n)? n
2 > 3 as char: F
2 > 3 as int : 0
not (2 > 3) :-1
2 > 3 as real: 0
not (2 > 3) :-1
</pre>
 
=={{header|Scala}}==
Booleans in Scala are given by the literals <code>true</code> and <code>false</code>, case sensitive, which are the only instances of the class <code>Boolean</code>.
Line 2,234 ⟶ 2,508:
is False
</pre>
 
=={{header|Sidef}}==
Sidef defines the ''true'' and ''false'' boolean values, which are part of the ''Bool'' type.
Line 2,369 ⟶ 2,644:
echo "false"
endif</syntaxhighlight>
=={{header|V (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.
 
Line 2,395 ⟶ 2,670:
true
false</pre>
 
=={{header|WDTE}}==
 
Line 2,412 ⟶ 2,688:
 
This class has two methods: the operator ''!'' which returns the logical complement of its receiver and ''toString'' which returns its string representation.
<syntaxhighlight lang="ecmascriptwren">var embed = true
System.printAll([embed, ", ", !embed, ", ", "Is Wren embeddable? " + embed.toString])</syntaxhighlight>
 
Line 2,419 ⟶ 2,695:
true, false, Is Wren embeddable? true
</pre>
 
=={{header|XLISP}}==
Boolean "false" may be represented by <tt>#F</tt>, <tt>#!FALSE</tt>, <tt>NIL</tt>, or the empty list; any other value is counted as true in conditional expressions, but it is also possible to represent the Boolean value "true" using your choice of the symbols <tt>#T</tt>, <tt>#!TRUE</tt>, and <tt>T</tt>. All these symbols are case-insensitive. Note that <tt>T</tt>, unlike the others, is a variable: it is bound by default to the constant <tt>#T</tt>, but you can (although you shouldn't) assign it any other value including "false" (by doing something like <tt>(setq t nil)</tt>). Boolean values are printed as <tt>#T</tt> and <tt>()</tt>.
Anonymous user