Boolean values: Difference between revisions

From Rosetta Code
Content added Content deleted
(added python)
Line 17: Line 17:


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

<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.

Revision as of 18:32, 10 July 2009

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. There are also object wrappers Boolean.TRUE and Boolean.FALSE which act in the same places, but have methods defined for them.

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.

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.