DATA TYPES

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Logic: Boolean

Example: true or false Size: 1 bit

Scope

The scope of variables can be local or global. local variables only work in the loop, procedure or class they are created in global variables can be accessed from any point in a program

Constants

Data values that stay the same every time a program is executed are known as constants. Constants are not expected to change. Literal constants are actual values fixed into the source code. An example of this might be the character string "hello world". The data value "hello world" has been fixed into the code. Named constants are values where a name is defined to be used instead of a literal constant. An example of this might be stating that the 'starting level' of a game is always referred to as 1. Examples of a constant within a game might be: the unit of gravity the number of lives available for the player the amount of time allowed for a level in a game

Declarations

Declaring a name for a variable is saying what the data type will be and where it will be stored in memory. Information about named constants and variables being used in a program is given in the form of declarations. These are sections of program code that define the names that will be used to refer to variables, and what kind of data each of them will hold.

Naming variables

Each variable is named so it is clear which variable is being used at any time. It is important to use meaningful names for variables: For example, pocketMoney = 20 means that the variable 'pocketMoney' is being used to store how much pocket money you have. Right now you have £20. The name given to each variable is up to the programmer, but ideally a variable name should have meaning, ie it should reflect the value that it is holding. Variable naming rules There are some rules about variable names: 1- Consistency: 'name' is not the same as 'Name' or 'NAME'. 2- Spacing: variable names should not have a space in them. Use underscores or camelCase instead, eg total_money; totalMoney). 3- Digits: variable names should not start with a digit

Character: One Charater

Example: a, F, 3, $, £, # Size: 1 byte Every letter, digit, and punctuation mark is a character. There are also many characters that are invisible on screen, such as the space, tab, and carriage-return characters. Most programming languages provide a data type called 'character' or 'char'. Each value represents a single character from a predefined set, such as ASCII or Unicode. Each character has its own binary pattern.

Character: Sequence (String)

Example: abc, hello world Size: Limited to the amount that can be stored in main memory A sequence of characters often stored as a variable in a computer program. Strings Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as "hello world". A string can contain any sequence of characters, visible or invisible, and characters may be repeated. The number of characters in the string is called its length, and "hello world" has length 11 - made up of 10 letters and 1 space. There is usually a restriction on the maximum length of a string. There is also such a thing as an empty string, which contains no characters - length 0. A string can be a constant or variable. If it is a constant, it is usually written as a sequence of characters enclosed in single or double quotation marks, ie 'hello' or "hello". Note that a string such as "573" is not the same as the number 573, because a string is a sequence of characters whereas a number is a sequence of digits. All keyboard input and text output is in the form of character strings.

Constants and variables

In a program, data values can be constant or variable. If values are variable they can be changed by the program and the user. When a program is run, the data values are held in memory whilst they are being worked on.

Assignment

In order to change the data value stored in a variable, you use an operation called assignment. This causes the value to be copied into a memory location, overwriting what was in there before. Different values may be assigned to a variable at different times during the execution of a program. Each assignment overwrites the current value with a new one.

Number: Floating point

Number Floating point (decimal number): Floating point numbers are numbers that have a fractional part, usually represented using two components: the main number and the fractional part, each of which is a binary number. This is known as a floating-point representation. Most programming languages provide one or more data types based on floating-point representations. They are usually given names like 'float', 'single', 'double', 'real' or 'longreal'. Example: 4.2, 27.4, 5.63 Size: 4 to 8 bytes

Number: Integer

Number Integer (whole number): Integers are whole numbers represented as binary values. Most programming languages provide a data type called 'integer', often called 'int' for short. Example: 4, 27, 65535 Size: 1 to 8 bytes

Input and output

Programs require data to be input. This data is used (processed) by the program, and data (or information) is output as a result. Data input Programs are written to solve problems. To solve a problem, a program needs data input and data, or information, output. Data can be input in different ways: Written directly into the program. This is called hard coding. By the user when the program is running. From a file or other source when the program is running. Consider this Python (3.x) program for calculating the perimeter of a square: side_length = 5 perimeter = side_length * 4 print(perimeter) The data in the variable 'side_length' has been hard coded, ie it has been written directly into the program. If we wanted to change this data, we would have to go back and change the program. It is usually best to avoid hard coding as much as possible because it is difficult and time-consuming to rewrite a computer program just to change a single value. Consider this alternative Python (3.x) program: side_length = input("Type in the side length: ") side_length = int(side_length) perimeter = side_length * 4 print(perimeter) This time, the data for the variable 'side_length' is input by the user when the program is running. The statement 'input' is used to tell the computer that the user must enter some data before the program can continue. Data output Once data has been processed, programs often need to output the data they have generated. In Python, the 'print' statement is used to output data. Consider this Python (3.x) program for calculating the perimeter of a square: side_length = input("Type in a side length: ") side_length = int(side_length) perimeter = side_length * 4 print("The perimeter of the square is: ") print(perimeter) This program uses the 'print' statement to: Display a message explaining what information is being output. Text is placed within quotes. Output the contents of the variable 'perimeter'. Variables are not placed within quotes. The print statement uses brackets to surround the data to be printed.

Variables

Sometimes we need computers to remember the information we give it and that it calculates during programs. A variable can be thought of as a box that the computer can use to store a value. The value held in that box can change or 'vary'. A program can use as many variables as it needs it to. All variables are made up of three parts: - a name - a type - a value Variables are a key element of programming. They are used for calculations, for storing values for later use, in decisions and in iteration. Variables are data values that can change when the user is asked a question, for example, their age. Variables may change during program execution. A variable is a memory location. It has a name that is associated with that location. The memory location is used to hold data. The key difference when comparing a constant to a variable is that the value associated with a variable name may change during program execution. For example 'highScore' would need to be variable to change throughout a game. The content and organisation of a computer's memory is not fixed - so neither is the value that is pointed at by a variable. When data is read from a variable, the content of the memory location is copied and used in calculations.

DATA TYPES

They are either numbers, characters or logical. Computer programs use data types to organise different types of data in a program. Data can also be constant or variable within programs and functions. Different types of data are represented in different ways inside a computer and need varying amounts of memory to store them. They also have different operations that can be performed upon them. All values that belong to the same data type will be represented in the same way. Some operations that can be applied to values of one data type obviously do not make sense when applied to values of another type. For example, we can calculate the square root of the integer 4, but not of the string "hello world".

Working with variables

Working with numbers You can use variables to store numbers. pocket_money = 20 The variable 'pocket_money' is used to store how much pocket money you have. Right now you have £20. As well as using fixed numbers in calculations and storing the answer in a variable, we can also use variables within the calculations themselves. In this example, the instruction uses the variable 'money_in_bank' to calculate the answer and then stores the answer in a variable called 'total_money'. total_money = money_in_bank + 10 The variables represent any value we choose to assign to them. Consider this Python (3.x) program: >>> money_in_bank = 20 >>> total_money = money_in_bank + 10 >>> print(total_money) 30 The first line stores the value 20 to the variable 'money_in_bank'. Since the computer knows that 'money_in_bank' has the value 20, it can calculate the value of 'total_money' and store it. Once calculated, the value of 'total_money' can be printed to the screen. Once a value is stored, all sorts of mathematical operations can be performed on the variables: >>> money_in_bank = 20 >>> total_money = money_in_bank + 10 >>> print(total_money) 30 >>> cost_of_holiday = 150 >>> left_to_pay = cost_of_holiday - total_money >>> print(left_to_pay) 120 Using variables means the exact amounts that are being used don't have to be remembered - the computer stores the numbers for us. Working with text A variable can hold a number, but it can also hold a piece of text. Just one letter is called a character. More than one character is called a string. A text variable works in the same way as a number variable, with a couple of differences: text variables hold characters (letters, digits, punctuation) the data in text variables is placed in quotes arithmetic calculations cannot be performed on text variables Consider the following Python (3.x) program that uses strings: >>> message = "Hooray! It's my birthday!" >>> print(message) Hooray! It's my birthday! Data in a character or string has quotes placed around it. This tells the computer that we are using text and not a number.


Ensembles d'études connexes

EXAM 3 - Mastering Bio Assignment #9

View Set

MCAT 10 most commonly missed concepts course

View Set

Module 8 Reading Assignment - Upper GI Disorders

View Set

Clinical Nursing Skills Nutrition

View Set

Chapter 31: Functional Assessment of the Older Adult - Questions

View Set

Science 701 Chapter 6 Lesson 5 Noninfectious Disease Review

View Set

US History Theme 5: American Expansionism Vocab

View Set