Boolean values

From Rosetta Code
Revision as of 19:48, 10 July 2009 by rosettacode>Glennj (→‎{{header|Perl}}: two single quotes starts italics)
Task
Boolean values
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to represent the boolean states "true" and "false" in a language. If other objects represent "true" or "false" in conditionals, note it.

BASIC

Works with: QuickBasic version 4.5

QuickBasic has no keywords for true and false. Boolean expressions evaluate to 0 when false, and a non-zero value when true. Numbers also work in place of boolean expressions following those rules.

Java

Java has true and false keywords, representing the only values of type boolean. There are also object wrappers Boolean.TRUE and Boolean.FALSE, of type Boolean which may be un-boxed into booleans (auto-unboxed in Java 1.5+). There are no automatic conversions from any other types into boolean, and it is a compile-time error to use any type other than boolean or Boolean in a place that expects a boolean (e.g. if-statement condition, while-statement condition, operand of a logical operator, etc.).

Perl

The values in Perl that are false are: 0 (as a number (including 0.0), or as the string '0' (but not the string "0.0"), the empty string "", the empty list (), and undef.

Everything else is true.

Perl has no special "true" or "false" keywords. [1]

Python

The values in Python that are false are: False, None, 0 (including 0 of all numeric types, and any object whose .__nonzero__() method returns false), and empty containers (including empty strings, lists, tuples, dictionaries, sets, etc., and any object whose .__len__() method returns 0).

Everything else is true. Constant True exists. [2]

Python

Python has a boolean data type with the only two possible values denoted by True and False.

The boolean type is a member of the numeric family of types, and when used in a numeric, but not boolean context, True has the value one and False the value zero. Conversely, when numbers are used in a boolean context, zero is False and anything other than zero is True.

In a boolean context, Python extends what is meant by true and false by accepting empty collection types, such as an empty dict or an empty list as being False, and non-empty collection types as being True, so in an if statement one might branch on a list which would be the same as testing if the list had any contents.

Python has special methods that can be used to set the boolean value of user-created objects too.

Some examples: <lang python>>>> True True >>> not True False >>> # As numbers >>> False + 0 0 >>> True + 0 1 >>> False + 0j 0j >>> True * 3.141 3.141 >>> # Numbers as booleans >>> not 0 True >>> not not 0 False >>> not 1234 False >>> bool(0.0) False >>> bool(0j) False >>> bool(1+2j) True >>> # Collections as booleans >>> bool([]) False >>> bool([None]) True >>> 'I contain something' if (None,) else 'I am empty' 'I contain something' >>> bool({}) False >>> </lang>

Ruby

The only values in Ruby that are false are: false and nil. They have synonyms FALSE and NIL.

Everything else (including the number 0 and the empty string) is true. Constants true (and TRUE) exist.

false, nil and true are singleton instances of classes FalseClass, NilClass and TrueClass respectively. [3]