Brief review of programming concepts: data types

Different amounts of storage space may be required for different types of data. Although which data types are supported may depend on the programming language, all will support numbers, strings, and Booleans. In many languages it is also possible to create your own type of data, which we will discuss later.

Numbers

Different amounts of space may be allocated to different types of numbers. In some languages there may be numbers that are stored exactly as they are entered (typically integers), and other numbers that are stored as floating point numbers, that is, approximations that fit into the amount of space allocated. It may also be possible to specify how precisely a number is to be stored.

Strings

Another common data type is a string, which is a sequence of characters. A string is typically demarcated by quotation marks, though what types of markers are used will depend on the programming language. This raises the question of how to put quotation marks inside a string: for special characters like this, special sequences, known as escape characters, are used. One common way of specifying an escape character is by putting a backslash before the special character.

image of a string

A string

The length of a string is the number of characters in it, including punctuation marks and blank spaces. For example, the string "A hot dog?" has length 10. The index of a character in the string is its position, where the first position is usually 0. As a consequence, the last position in the string is one less than the length of the string.


image of a substring

A substring

At times it will be useful to deal with parts of strings. A substring is formed by chopping off zero or more characters from the end and zero or more characters from the beginning of the string. Here "ill" is a substring of "caterpillar".

Special types of substrings include a prefix of a string, formed by chopping off zero or more characters from the end of a string, and a suffix, formed by chopping off zero or more characters from the beginning of the string. In our example, "cat" is a prefix and "pillar" is a suffix.

Booleans

Unlike numbers and strings, for which there are many different possible values, a Boolean is a data typeA type of data, such as a number or a string. with just two values, true and false. Just as you can form an arithmetic expression out of numbers and operations, you can form a Boolean expression out of Boolean values and functions. Complex expressions can be used to help make decisions about what to do, with one course of action taking place if the expression is true and another if the expression is false.