Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.

Similar presentations


Presentation on theme: "Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from."— Presentation transcript:

1 Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from the keyboard or can be the result of a calculation. A variable like name would hold data that is text. We sometimes call this a string. A variable like high score or exam mark would hold data that is a number. Different data types are therefore needed to store different types of data.

2 Data Types Integer This data type is used for storing a positive or negative whole number Example : 12, 563, 0, -83 This data type is used to store e.g. an age, the number of tickets you want to buy or the number of people at a sports event. Real This data type is used for storing a positive or negative number which contains a decimal or fractional part. Example : , , 1.64 This data type could be used to store a person’s height or time in a race.

3 Data Types String (Text)
This data type is used for storing a data item of text. Example : “Yes”, “G62 7HS”, “Glasgow” This data type would be used to store e.g. a person’ s name, address or postcode. Boolean This data type is a two state variable and it can only store True or False. Example : True or False A Boolean variable could be used to say whether an item of data has been found in a list.

4 Data Structures An array is an example of a data structure. It is used to store a list of items that are of the same data type. For example a list of names, a list of teams or a list of employees. Now read pages 10 and 11 in your book and answer Question 3 on page 34 and 35.

5 Errors in Programs Most programs contain errors while they are in development. The programmer must ensure the code is tested as much as time allows. Testing should be competed in logical planned manner. Test data should be thought out and expected results should be compared against actual results. There are 3 types of errors in programs. These are called syntax, execution and logic errors.

6 Syntax Errors INPUT name$ PRINT “Hello; name$ END
Syntax errors occur when the programming language rules have not been followed. Often this is caused by a typing error. For example a misspelt keyword or missing bracket or missing keyword. These errors are highlighted when the program is translated by the interpreter or compiler. True Basic Example : Syntax Error PRNT “Please enter your name” INPUT name$ PRINT “Hello; name$ END

7 Execution Errors PRINT “Please enter a number“ INPUT number
If the program compiles and there are no syntax errors the program can still produce errors when it is run. Examples of execution errors are dividing by 0, indexing an array by a index outside of the array size. True Basic Example : Execution Error PRINT “Please enter a number“ INPUT number LET fraction = number / 0 PRINT “Your number divided by 10 is”;fraction END

8 Logic Errors A program can have no syntax errors and no execution errors. It can run to completion without crashing but it does not produce the expected result. This is known as a logic error. It may be that the code multiplies 2 numbers instead of adding them. It may be that in a calculation BODMAS has not been applied to the formula and it produces the wrong answer. True Basic Example : Logic Error PRINT “Please enter the length of the rectangle” INPUT length PRINT “Please enter the breadth of the rectangle” INPUT breadth LET area = length + breadth PRINT “The area is “; area END

9 Now read pages 22 and 23 in your book and answer Question 1 on page 38.

10 Readable Code It is good programming practice to make your code readable. This makes it very easy for another programmer to maintain the code. Programs are made readable by using a variety of techniques. Internal commentary – comments the computer ignores but helps us to understand what the code does. Meaningful variable names – giving a variable a name that is appropriate to what data is stored in that variable. Indentation and blank lines – help to give a program a structure so that it is easier to read. The code on the next slide demonstrates these 3 techniques:

11 Readable Code ! For loops ! This program asks the user for their name and a sentence ! It then asks for the number of times they want to display ! the sentence and it then displays the sentence that number of times. PRINT "Please enter your name" INPUT name$ PRINT "please enter your sentence" INPUT sentence$ PRINT "Please enter the number of times you wish to print your sentence" INPUT number PRINT "Here are your sentences "; name$ FOR counter = 1 to number PRINT counter;sentence$ NEXT counter END Internal commentary – lets us know what the program does. Meaningful variable names – easy to see what is stored in each variable Indentation and white space – format the program so it is easier to read

12 Now read pages 23 in your book and answer Question 4 on page 39.

13 Testing Testing your program makes sure that it performs in the way that you expect it to. It is a very important part of the development of software. It should be planned in a logical manner. This means that test data should cover normal, extreme (boundary) and exceptional cases. When showing that you have tested a program you should indicate what conditions you tested, what results you expected and the actual results that were achieved when you ran the program with those conditions. This is usually displayed in a test table.

14 Testing Normal This type of test ensures that the program does what it should do when used with normal or expected input.   Extreme (sometimes referred to as limit or boundary) This type of test ensures the program does what is expected with extreme input. Exceptional This type of test ensures that the program can handle data that is exceptional. 

15 Testing ! This program asks the user for a mark
Example: This program asks the user for a mark given for exam. The mark must be between 0 and 100. ! This program asks the user for a mark ! and validates that mark (0 and 100) DO PRINT "Please enter the mark“ INPUT mark IF mark <0 or mark >100 THEN PRINT “You have entered an invalid mark.” PRINT “Please try again“ END IF LOOP UNTIL mark>=0 and mark<=100 END Normal Test Data 9, 23, 45, 83 Extreme Test Data 0, 100 Exceptional Test Data -5, 128, a

16 Testing Test Data Type Expected Output Actual Output 9 Normal
Test tables are usually written to indicate what data has been tested. Test table should be laid out in the following way. Test Data Type Expected Output Actual Output 9 Normal Accepted and stored 45 Extreme 100 -5 Exceptional Error Message a 128 What do you expect your program to do when it is run with this data. What actually happens when your program is run with this data. The data that you use when you run the program. The type of test data : should be Normal, Extreme or Exceptional.

17 Testing You would hope that the Expected Output column and the Actual Output column match.

18 Now read pages 24 and 25 in your book and answer Question 2 and 3 on page 38.

19 Want to know why code has to be both readable and well tested?
Imagine the astronauts whose lives depended on her code being properly tested... Imagine the NASA software developers that came after her... ... trying to read thru 16 books of hand written code?


Download ppt "Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from."

Similar presentations


Ads by Google