Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming constructs

Similar presentations


Presentation on theme: "Programming constructs"— Presentation transcript:

1 Programming constructs
The building blocks of programming

2 Naming and storing data
A program is made up of two main parts: a series of instructions the data that will be used. When programming you need to define: where each item of data will be stored what name to give to each item of data. Data can be stored as variables or constants: variables are data items where the values may change as the program is executed constants are data items where the value will stay the same as the program is executed. © 2005 Bob Reeves, Dave Fogg/Hodder Murray

3 Variables and constants
Any item of data where the value may change. For example, a system for recording student grades would have two variables: Student Name Grade. The values assigned to each variable will change as data is entered into the system. In this example it will change every time a new student name and grade are entered. Constants Any item of data where the value remains the same. For example, a system for calculating VAT in invoices would have one constant: VAT set at 17.5%. This value would not change as the program is run as the rate of VAT does not change. © 2005 Bob Reeves, Dave Fogg/Hodder Murray

4 Data types (1) When data is stored in a program, you must specify what type of data it is so that the program knows how to handle it. For example, you might perform the calculation "2" + "2" but unless you specify that these two items of data are numeric, the answer will be "22". Different programming languages use different names for data types but the basic types are common to all languages. © 2005 Bob Reeves, Dave Fogg/Hodder Murray

5 Data types (2) Integer: stores whole numbers that may be positive or negative. Real: stores numbers including the use of decimal places – also known as single or double in some languages. Text: stores text or text/numbers – also known as alphanumeric or string in some languages. Boolean: stores data that is either yes/no or true/false. Currency: stores values as pounds and pence. © 2005 Bob Reeves, Dave Fogg/Hodder Murray

6 How to use the data types
A private hospital creates a system to record details about patients. It may use the following variables and data types. A yes/no answer is required Boolean Has the customer paid? Represented as UK sterling Currency Cost of treatment Need to use decimal places Real Height Will be a whole number Integer NumberOfVisits Name is made up of text String Name Explanation Data type Data item © 2005 Bob Reeves, Dave Fogg/Hodder Murray

7 Declaring variables and constants
When you create a program you need to tell the computer (declare) what variable and constants you will be using. It is good practice to declare all the variables and constants that the program will use before you start writing the code. Using the hospital example, the declaration (using VB in this case) would look like this: Dim CustomerName As String Dim NumberOfVisits As Integer Dim CustomerHeight As Single Dim CustomerPaid As Boolean Dim TreatmentCost As Currency Const VAT = 17.5% Dim is short for Dimension and is the term used by VB. Other languages use different terms. VAT has been declared as a constant at 17.5%. The users can also define their own variable types. © 2005 Bob Reeves, Dave Fogg/Hodder Murray

8 Issues to consider when declaring variables
Give variables sensible and meaningful names so that you know what they are later on. In large programs, it is useful to split your code into more manageable sections. Decide whether you want to use each variable in every part of the program or just in certain parts of the code. Local variables are only available in the section of code where they have been declared. Global variables are available everywhere in the program. © 2005 Bob Reeves, Dave Fogg/Hodder Murray


Download ppt "Programming constructs"

Similar presentations


Ads by Google