6Data Types
6.1. Basic types
The languages Exp2 , . . . , Lang3 that we used to describe the notions of semantics and typing only consider two data types: integers and booleans. However, most programming languages include many more basic types. In this section, we shall present the most widespread data types, along with the ways in which they differ between languages.
6.1.1. Booleans
For certain languages, such as OCaml, Java, Ada and Pascal, the boolean type bool is a constructor of the type algebra and has two possible values: true and false. Other languages, such as C and C++, encode boolean values as integers, with a value of 0 conventionally used to mean “false” and any other value corresponding to “true”. Some of these languages, such as C++, include a type bool and predefined identifiers false and true but these values are fully compatible with integer values. Any integer expression may be used as the condition for a loop or an if-then-else. Any boolean expression may be used as an index in an array, or as an operand in an arithmetic operation. This makes the source text harder to understand and may lead to errors, which are difficult to detect.
Python behaves somewhat differently. It supplies two values, True and False,but allows integers and booleans to be mixed. It is thus possible to test whether or not an integer value is equal or different to a boolean value, or to use an integer value as the test for a condition. These practices are not recommended, see the examples ...
Get Concepts and Semantics of Programming Languages 1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.