varaibles

Page 1

Variables and Data Types A variable is a storage location in the memory of the computer. A variable is usually given a name to make it easy to refer to (meaningful variable names). Data can be stored in a variable at any time during the execution of a program. When variables are created they are normally able to store one particular type of data. A variable cannot normally be used to store data of any type other than the type for which it was created. Data Types To avoid silly mistakes like trying to multiply two letters together, languages tend to classify data into distinct types, and restrict the range of operations which can be performed on any given type. Different data types are used for two reasons. Firstly, each data type is represented differently internally and the compiler or interpreter can tell immediately how much memory to set aside for each one. Secondly, using of data types allows the programmer to validate data and this aids the robustness of the software. There are two main families of data types available to the programmer, simple (or scalar) data types and structured (or complex) data types. Simple Data Types Integer An integer value may be any positive or negative whole number. Examples of integers are: 0, 17, -3, 876 Real The definition of a real value is not so clear cut as the integer. A real value can be any number. In most programming environments, a real value is taken to mean a number with a fractional part. Examples are: 2.0, 3.9, -789.5432 A real number is normally represented internally by the machine as a floating point binary number. Some environments actually name the type 'floating point' to avoid ambiguity. Some environments allow integers to be treated as a subset of real numbers. For example, the number 4, stored as a real number rather than an integer, might be represented by 3.9999999 or 4.0000001 Character A character value is taken to mean any single character, usually from the ASCII character set. Characters are usually differentiated from number values by enclosing them in quotes. Examples of character values might be: a, $,, %, Z Boolean A boolean value is a two-state value. It can either be True or False. This data type is a useful tool for terminating loops and satisfying conditions within a program. Most software development environments provide facilities for storing simple data types such as, integer, real, character and boolean.

Higher Computing - Soft Dev - High Level Programming Language Constructs Produced by S Lambert, R Simpson and HSDU for The City of Edinburgh Council, 2004

Infosheet 1.1


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.