3.2 Programming Flashcards

1
Q

The three combining principles that are basic to all high-level imperative programming languages.

A

sequence, iteration/repetition and selection/choice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The three programming constructs:

A

Sequence, selection, iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Two types of iterations (2 names each), also known as

A
definite iteration (also known as count-controlled iteration)
indefinite iteration (also known as condition-controlled iteration)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sequence is

A

Sequence is the order in which programming statements are executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is the sequence of a program important?

A

The sequence of a program is extremely important as once these are translated, carrying out instructions in the wrong order leads to a program performing incorrectly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In this pseudo-code program, designed to find the average of two whole numbers, there is an error. What kind?

total ← 0 
average ← number1/number2
OUTPUT “Enter the first number”
number1 ← USERINPUT
OUTPUT “Enter the second number”
number2 ← USERINPUT 
OUTPUT "The average is " + average
A

Running this program would result in a logic error, because it tries to calculate the average before it knows the values of the numbers.

This version has the instructions in the correct sequence:

total ← 0 
OUTPUT “Enter the first number”
number1 ← USERINPUT
OUTPUT “Enter the second number”
number2 ← USERINPUT
average ← number1/number2
OUTPUT "The average is " + average
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Selection is?

- it is the process of?

A

Selection is a programming construct where a section of code is run only if a condition is met.

  • Selection is the process of making a decision.
  • The result of the decision determines which path the program will take next.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does selection work?

A

Selection works by testing a condition. The test gives a Boolean result - TRUE or FALSE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In programming, how is selection implemented? - so how would it be written in pseudo-code

A

In programming, selection is implemented using IF THEN ELSE statements:

OUTPUT “How old are you?”
age ← USERINPUT
IF age > 16 THEN
OUTPUT “You are old enough to drive a car”
ELSE
OUTPUT “Come back when you are older!”
ENDIF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Iteration is

A

The repetition of a block of statements within a computer program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Iteration is often referred to as _ (2)

A

Iteration is often referred to as looping, since the program ‘loops’ back to an earlier line of code. Iteration is also known as repetition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why iteration is helpful?

A

Iteration allows programmers to simplify a program and make it more efficient . Instead of writing out the same lines of code again and again, a programmer can write a section of code once, and ask the program to execute the same line repeatedly until no longer needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When a program needs to iterate a set number of times, this is known as __ iteration and makes use of a __ loop.

A () loop uses an extra variable called a __ __ that keeps track of the number of times the loop has been run.

A

Definite iteration
FOR loop

A FOR loop uses an extra variable called a loop counter that keeps track of the number of times the loop has been run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

This program would print a message out six times:

OUTPUT “Coding is cool”
OUTPUT “Coding is cool”
OUTPUT “Coding is cool”
OUTPUT “Coding is cool”
OUTPUT “Coding is cool”
OUTPUT “Coding is cool”

Improve efficiency using iteration.

A

Definite iteration (also known as count-controlled iteration):

FOR count ← 1 TO 6
OUTPUT “Coding is cool”
ENDFOR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

FOR count ← 1 TO 6
OUTPUT “Coding is cool”
ENDFOR

What kind of variable is ‘count’ ? (not important)

A

Stepper variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use iteration to print out the ten times table, from 1 to 10.

A

The stepper variable used to initialise a FOR loop can be used within the loop itself. This program uses a loop’s condition variable to print the ten times table:

FOR count ← 1 TO 10
OUTPUT count * 10
ENDFOR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does iteration simplify a program? (2)

A

As can be seen above, by using iteration a program is simplified, less error prone and more flexible. This is because:

There are fewer lines of code, meaning fewer opportunities for typing errors to creep in

To increase or decrease the number of iterations, all the programmer has to do is change the loop’s end value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is STEP?

A

It is also possible to add the keyword STEP to the first line of a FOR loop to determine how much the stepper variable increases or decreases by with each iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Code to output 1, 3, 5, 7, 9 as the value of count increases by two for each iteration

A

FOR count ← 1 TO 10 STEP 2
OUTPUT count
ENDFOR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Code to display 5, 4, 3, 2, 1

A

The code below would display 5, 4, 3, 2, 1 as the value of count decreases by one for each iteration:

FOR count ← 5 TO 1 STEP -1
OUTPUT count
ENDFOR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does indefinite iteration do

A

Indefinite iteration repeatedly executes a section of code until a condition is met - or no longer met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

There are two types of indefinite iteration:

A

There are two types of indefinite iteration:

WHILE loops - uses the statements WHILE and ENDWHILE

REPEAT UNTIL loops - uses the statements REPEAT and UNTIL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Print a string six times, using a while loop

A
count ← 0
WHILE count < 6
     OUTPUT “Coding is cool”
     count ← count + 1
ENDWHILE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

When do WHILE loops test the condition is met

A

At the beginning of the loop.

If the condition is met, the code within the loop is executed before the program loops back to test the condition again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Scope of the while loop?
The WHILE statement defines the start of the loop. The ENDWHILE statement declares the end of the loop. By combining the start and the end, the scope of the statement is identified.
26
When does a while loop stop?
The iteration continues until the condition test result is FALSE, at which point the loop ends and the program executes the next line of code in sequence after the loop.
27
Is it possible for the code in a WHILE loop to never be executed.
Because the condition is tested at the start of the loop, it is possible for the code within it to never actually be executed. Consider this program: ``` password ← USERINPUT WHILE password <> “B1t3s1z£” OUTPUT “Password incorrect. Try again.” password ← USERINPUT ENDWHILE ```
28
Are while loops useful for validation?
Yh Consider this program: ``` password ← USERINPUT WHILE password <> “B1t3s1z£” OUTPUT “Password incorrect. Try again.” password ← USERINPUT ENDWHILE ``` The first time the condition is tested, the result may be FALSE if the value of password matches. Because of this, none of the code within the loop will be executed and the program will move onto the next line of code in sequence after the loop. WHILE loops are particularly useful for validation of user inputs as the code can insist that they retry entering data until it is correct.
29
Difference between a WHILE and REPEAT UNTIL loop
REPEAT UNTIL loops function in the same way as WHILE loops, with one major difference - the condition is tested at the end of the loop
30
Print a string ten times using repeat until loop
``` count ← 0 REPEAT OUTPUT “Coding is cool” count ← count + 1 UNTIL count = 10 ```
31
Is it possible for the code in a REPEAT UNTIL loop to never be executed.
No. The REPEAT statement defines the start of the loop. The UNTIL statement tests the condition and declares the end of the statement scope. Because the condition is tested at the end, the code within the loop is always executed at least once, even if the result of the test is FALSE.
32
Key difference between WHILE and REPEAT UNTIL loop in terms of function
With a WHILE loop, the code within the iteration may never be executed. With a REPEAT UNTIL loop, the code is always executed at least once.
33
Another type indefinite (condition controlled) iteration with the condition at the end - according to spec (not repeat until)
DO ... Instructions here ... WHILE NotSolved
34
Use nested iteration to print out the times table for all numbers from one to ten
Iteration can also be nested. This program uses two definite FOR loops, one within another, to print out the times table for all numbers from one to ten: ``` FOR x ← 1 TO 10 FOR y ← 1 TO 10 result ← y * x OUTPUT y + " * " + x + " = " + result ENDFOR ENDFOR ```
35
What can be in nested *iteration*?
Nested iteration isn't limited to FOR loops. A WHILE loop can be nested in a FOR loop and a FOR loop can be nested in a WHILE loop.
36
An example of nested iteration would be: | Using while and for
An example of nested iteration would be: ``` WHILE NotSolved ... Instructions here ... FOR i ← 1 TO 5 ... Instructions here ... ENDFOR ... Instructions here ... ENDWHILE ```
37
An example of nested selection would be:
An example of nested selection would be: ``` IF GameWon THEN ... Instructions here ... IF Score > HighScore THEN ... Instructions here ... ENDIF ... Instructions here ... ENDIF ```
38
Why nested selection can be good?
Nested selection can be built up over and over to include many possibilities and paths for a program to follow. It allows for complex decisions to be made. (Strange answer)
39
The grade of a student depends on the marks gained in an exam. The grade bands are Grade C if the marks are less than or equal to 20% Grade B if marks are more than 20% and less than or equal to 60% Grade A if marks are more than 60% Write pseudo-code using nested something
The pseudocode below makes use of nested IFs to work out the grade Mark USERINPUT("Enter your exam result as a percentage") IF Mark <= 20 THEN PRINT 'You obtained a C grade' ELSE IF Mark > 20 AND Mark <= 60 THEN PRINT 'You obtained a B grade' ENDIF ELSE PRINT 'You obtained an A grade' ENDIF
40
Does nested ifs have to be used?
No. IF-THEN-ELSE IF ENDIF (removes the need for multiple indentation levels) - see aqa pseudo-code syntax pdf - could not copy it here properly (but two programs are same, but one makes use of nested, while other does not)
41
A variable is
A variable is a named piece of memory that holds a value. The value held in a variable can - and usually does - change as the program is running.
42
A variable's name is known as
Identifier
43
Identifier naming conventions (5)
It can contain letters and numbers but must start with a letter. It must contain at least one letter - at the start of the name. It must not contain special characters such as !@£$%&* or punctuation characters. However, an underscore can be used. Spaces are not allowed. It should contain lowercase letters. However, uppercase letters can be used if a variable name comprises more than one word joined together. The name should be meaningful - it should represent the value it is holding.
44
Use meaningful identifier names and know why it is important to use them. What should the name do?
It is good practice in programming to give variables a sensible name that will help explain its purpose.
45
Link between variables and memory locations (not important?)
Variables make it easy for a programmer to use memory locations. The computer keeps track of which memory location the variable refers to. All the programmer has to do is remember the identifier the variable was given.
46
Variable Declaration
Most programming languages require a variable to be identified before a value is assigned to it. This is known as declaring a variable. Some programming languages, such as Python, do not require variables to be explicitly declared before use.
47
Giving a variable a value is known as __
Assignment
48
A constant is?
A constant is a named piece of memory where the value cannot be changed while a program runs.
49
Why constants are useful
Constants are useful because they are declared and assigned once, but can be referred to over and over again throughout the program. This means that if the programmer needs to change the value throughout the program code, they only need to make one change. This can help make a program easier to maintain.
50
Constant naming convention
Constants follow the same naming conventions as variables, except that they are often written in uppercase. Some programming languages, such as Python, do not support constants. Constants are used for values that are unlikely to change, for example: CONSTANT PI ← 3.141 (AQA pseudo-code ‘names of constants will always be written in capitals’)
51
Global vs local variables?
A global variable is one that can be accessed and changed throughout the whole program. Local variables only exist within a particular subroutine.
52
Advantages of local variables (2) - guessing there will be another question like this, with better answers later
Using local variables rather than global variables makes it easier to debug a program as the value of that variable can only be read or changed within one subroutine. Another advantage of local variables is memory efficiency. Once a subroutine has finished running, the memory used for all local variables is removed.
53
27 Data type and purpose?
Integer Whole numbers
54
27.5 Data type and purpose
Real Decimal numbers
55
A Data type and purpose
Character A single alphanumeric character
56
ABC Data type and purpose
String One or more alphanumeric characters
57
TRUE Data type and purpose
Boolean TRUE/FALSE
58
Limitations of different data types (2)
integers and real numbers cannot be concatenated, ie joined together. numbers held in strings cannot be subject to mathematical operations.
59
Casting
Changing the data type of a variable
60
Operator
A character, or characters, that determine what action is to be performed or considered.
61
3 types of operator that programmers use. These operators are common to most __-__ __ __.
There are three types of operator that programmers use: - arithmetic operators - relational operators - Boolean operators These operators are common to most high-level programming languages.
62
Arithmetic operators allow
Arithmetic operators allow numerical operations to be performed on values
63
``` Arithmetic operation Operator Example Addition Subtraction Multiplication Real division Integer division Remainder ```
``` Arithmetic operation Operator Example Addition + x = x + 5 Subtraction - x = x - 5 Multiplication * x = x * 5 Real division / x = x / 5 Integer division DIV x = x DIV 5 Remainder MOD x = x MOD 5 ```
64
Relational operators allow
Relational operators allow for assignment and for comparisons to be made. They are used in condition testing such as IF statements and loops.
65
Remember the differences in pseudo-code and python for relational operators.
.
66
Boolean operators are used
Boolean operators are used to combine relational operators to give more complex decisions.
67
3 Boolean operators
AND OR NOT ``` IF x > 0 AND x < 10 IF topic = "Computing" OR topic = "Computer Science" WHILE NOT (a < b) // WHILE NOT x ```
68
What is an array
An array is a data structure that holds similar, related data
69
Each ‘what’ in an array | Each Piece of data within the array is ‘what’
(An) Element
70
Each element has a position in the array, and can hold a value. What is the key thing about data in an array>
The data in an array must all be of the same data type.
71
What is an array a substitute for?
Sometimes a programmer needs to store a lot of related data. For example, a game might record the scores achieved by players. One way to do this would be to declare a variable for each score. So for ten scores, the game program would require ten variables: score1 score2 score3 And so on, up to score10 While certainly possible, this is not a practical method of recording such data. Suppose the program needed to record 100 scores? 100 variables would be required! A better method to solve the problem above is to use an array.
72
What must be done, before an array can be used?
Before an array can be used, it must be declared.
73
To declare an array a programmer gives it at least two properties:
an identifier a size - the number of elements it will hold
74
Once declared, can the name and structure of an array be changed?
No
75
Is it possible to declare an array without setting the size first?
Yes. It is possible to declare an array without setting the size first. This is called a dynamic array. To use a dynamic array, data must be added to the end of the array, or a new size set each time more data is added. In pseudo-code, declaring an array before use is not needed.
76
How to assign a value to an element in an array
Values are assigned to an element in an array by referring to the element's position.
77
Assign the value 100 to the first element in the array ‘score’
score[0] ← 100 - would assign the value 100 to the first element in the array
78
How to overwrite values in elements in an array
Values in elements can be overwritten at any point, simply by assigning another value to that element.
79
How to retrieve values from an array
Values are retrieved from an element in the array by again referring to the element's positioN
80
Output the the eighth value held in the array ‘score’
OUTPUT score[7]
81
What’s a two-dimensional array?
A two-dimensional array can hold more than one set of data. This type of array is like a table, with data held in rows and columns. The following array would hold ten scores for two players. The first player (0) has data stored in the first row. The second player (1) has data stored in the second row. 0 1 2 3 4 5 6 7 8 9 0 100 110 85 80 92 72 66 98 100 120 1 90 99 102 88 78 100 67 120 88 105
82
Declaring a 2D array
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
83
What does indexing in an array start as
0
84
2D array, fill in gaps Identifier[?][?]
Identifier[row][column]
85
table ← [[1, 2],[2, 4],[3, 6],[4, 8]] table[3][1] Result?
table ← [[1, 2],[2, 4],[3, 6],[4, 8]] table[3][1] evaluates to 8 as second element # (with index 1) of fourth array # (with index 3) in table is 8
86
How to repeat statements the number of times that there are elements in an array?
FOR Identifier IN array # statements here ENDFOR primes ← [2, 3, 5, 7, 11, 13] total ← 0 FOR prime IN primes ENDFOR OUTPUT 'Sum of the values in primes is' OUTPUT total
87
Make 7 into 11: MyArray ← (3,56,21,5,7)
MyArray ← (3,56,21,5,7) MyNumber ← 11 MyArray[4] ← MyNumber
88
Declare an empty array called myArray
array MyArray [] | Ignore?
89
Advantage of array
An advantage of array is that a number of sorting, searching and updating functions can be applied to it.
90
Length of myArray in pseudo-code
LEN(myArray)
91
Output each element in an array using length function
loopcount ← LEN(MyArray) - 1 FOR x ← 0 TO loopcount: OUTPUT MyArray[x] ENDFOR
92
Write a program that requests the user to input a list of their favourite animals. The user types in 'end' to indicate that the list is complete. This data needs to be stored and then printed out once the input is complete. In Python
``` favAnimals = [] index = 0 ``` currentAnimal = "" print("Enter your favourite animals. Enter 'end' when done.") while currentAnimal != "end": currentAnimal = input() favAnimals.append(currentAnimal) print("Your favourite animals are: ") for animals in favAnimals: print(animals)
93
How to delete item at given index in python
list.pop(index)
94
thislist = ["apple", "banana", "cherry"] Remove ‘banana’, be deleting its index
thislist = ["apple", "banana", "cherry"] thislist.pop(1) print(thislist) #[‘apple’, ‘cherry’] outputted
95
thislist = ["apple", "banana", "cherry"] Remove the last item.
thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist) Or thislist = ["apple", "banana", "cherry"] thislist.pop(-1) print(thislist) #[‘apple’, ‘banana’] outputted
96
thislist = ["apple", "banana", "cherry"] Clear the list
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
97
thislist = ["apple", "banana", "cherry"] Remove ‘banana’ using ‘banana’
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
98
Two dimensional array also known as a __. | Where one-dimensional arrays are __, two dimensional arrays are __.
Matrix Lists Tables
99
Declaring a 2D array pseudo-code, 3 rows, 4 columns : weird
My2DArray [3] [4] - teachICT array score [1,9] - would give an array with two rows and ten columns - bite size ???
100
A 2D array is ideal for
A 2D array is ideal for storing any data that has been arranged in a grid - for example the computer screen you are reading right now is made up of pixels, and any pixel can be identified by its vertical and horizontal location
101
Declare a 2D array, with 600 rows and 800 columns Initialise the structure so that every element is white - this is value 255
screen ← [600] [800] #Wierd ``` FOR row ← 0 to 599 FOR col ← 0 to 799 screen [row] [col] ← 255 ENDFOR ENDFOR ```
102
5 common actions that can be done on arrays
append - add an item to the end of an array insert - insert an item at a specific location in the array, all the data above is moved up one location delete - remove an item from a location, then all the data moves down one place. split - split an array at a given point and create two new arrays. slice - take a chunk of data out of an array to form a new array, the original array remains intact
103
What do records allow?
Records allow data values of different types that relate to each other to be stored together instead of in different arrays.
104
Record declaration pseudo-code
``` RECORD Record_identifier field1 : field2 : ... ENDRECORD ```
105
Record for a car: make, model, reg, price, noOfDoors
``` RECORD Car make : String model : String reg : String price : Real noOfDoors : Integer ENDRECORD ```
106
Pseudo-code record variable initialisation
varName ← Record_identifier(value1, value2, ...)
107
``` RECORD Car make : String model : String reg : String price : Real noOfDoors : Integer ``` Initialise a ford: pseudo-code
myCar ← Car('Ford', 'Focus', 'DX17 | GYT', 1399.99, 5)
108
Assigning a value to a field in a record - pseudo-code
varName.field ← Exp
109
myCar ← Car('Ford', 'Focus', 'DX17 GYT', 1399.99, 5) Change model to fiesta, pseudo-code
``` myCar.model ←'Fiesta' # The model field of the myCar # record is assigned the value # 'Fiesta'. ```
110
Accessing values of fields within records - pseudocode
varName.field
111
What does this do? (Records topic) OUTPUT myCar.model
OUTPUT myCar.model ``` # Will output the value stored in the # model field of the myCar record ```
112
4 differences between records and arrays
An array tends to have the same data type for every element, records often have mixed data type fields The number of fields within a record is fixed once it is declared, they cannot be added to or deleted as the program runs. A record declaration does not contain actual data - it is a template with which to create variables of that type. Arrays can be used to store records with each element in the array of the same record type.
113
String concatenation, how?
Concatenation means to join things together. You can do this with strings using the '+' operator. Like this :- Line 1: FirstName ← input("Enter your first name") Line 2: Surname ← input("Enter your surname") Line 3: Message ←"Your name is " + FirstName + " " + Surname Line 4: output(Message)
114
What do you need to remember when concatenating (2)
- same data type (string) | - you must manually add a space if you want it
115
Length ? Python, pseudo-code
len() LEN()
116
Casting to integer, string, real in python, character, code
``` int(item) casts the item into an integer str(item) casts the item into a string float(item) casts the item into a floating point real number chr() ord() ```
117
Purpose of ‘import random’
It imports the random library(/module) so that… (random integers can be generated)
118
What are subroutines?
Subroutines are smaller, named sections of code that are written within a larger program. The purpose of a subroutine is to perform a specific task. This task may need to be done more than once at various points in the main program.
119
Advantages of subroutines explained (3)
Subroutines are usually small in size, which means they are much easier to write, test and debug. They are also easy for someone else to understand. As they are written outside of the main program, subroutines can be saved separately as modules and used again in other programs. This saves time because the programmer can use code that has already been written, tested and debugged. A subroutine may be used repeatedly at various points in the main program and changed by passing in data known as parameters. However, the code only has to be written once, resulting in shorter programs.
120
2 types of subroutine
There are two types of subroutine: procedures functions
121
What is a procedure?
A procedure is a subroutine that performs a specific task. When the task is complete, the subroutine ends and the main program continues from where it left off. For example, a procedure may be written to reset all the values of an array to zero, or to clear a screen.
122
Procedure pseudo-code syntax
SUBROUTINE identifier (parameters) procedure code ENDSUBROUTINE
123
How is a procedure run?
A procedure is run by calling it. To call it, a programmer uses the procedure name and includes any parameters (values) that the procedure needs, for example: clear_screen(5)
124
What’s a parameter?
In computer programming, a parameter is a value that is passed into a function or procedure.
125
What is function?
A function works in the same way as a procedure, except that it manipulates data and returns a result back to the main program.
126
Fucntion example pseudo-code
For example, a function might be written to turn Fahrenheit into Celsius: SUBROUTINE f_TO_c(temperature_in_f) temperature_in_c ← (temperature_in_f –32) * 5/9 RETURN temperature_in_c ENDSUBROUTINE
127
How is a function run
A function is run by calling it. To call it, a programmer uses the function's identifier, the value to be passed into the function, and a variable for the function to return a value into, for example: celsius ← f_to_c(32)
128
Procedures vs function difference (in purpose)
Procedures perform a specific task but do not return a value. Functions manipulate data and return a value to the main program.
129
Procedures vs function difference (in code)
AQA pseudo-code does not use a different keyword to show the difference between a procedure and a function. Instead, it adds a RETURN statement to show that a subroutine is a function.
130
2 levels to variable scope
Global, Local
131
Why use local variables (5) - advantages
1. global variables waste memory 2. variable names can be unique 3. global variables can cause bugs local variables 4. start as null each time 5. global variables are hard to trace declaring variables in a subroutine means that memory allocation is only needed while the subroutine is executing – if the variables were all declared globally, then unnecessary memory would be used whereas local variables are removed from memory once the subroutine has completed local variables only need to be unique within the subroutine rather than throughout the whole program – this means that a programmer does not need to know the names of variables used within other subroutines local variables cannot be changed by another subroutine which could interfere with the way a program work when a subroutine is executed, all local variables are added to memory with no data so any previous data from a previous execution of a subroutine will not affect the result ..because they can be manipulated by multiple subroutines
132
Why design robust programs?
When writing code, it is important to ensure that a program runs correctly and continues to run no matter what actions a user takes. This is done through planning for all possibilities and thinking about what a user may do that the program does not expect.
133
Designing robust programs encompasses three areas: This anticipation and protection is done through:
protection against unexpected user inputs or actions, such as a user entering a letter where a number was expected confirming that users on a computer system are who they say they are minimising or removing bugs This anticipation and protection is done through: validation authentication testing
134
What does validation do
A programmer should consider that any inputs a user makes may be incorrect and should plan for such unexpected actions. Using validation helps a programmer to ensure that any data input is possible and sensible.
135
5 types of validation
Range check - the input must fall within a specified range. This is usually applied to numbers and dates, but can apply to characters too. For example, when a payment needs to be made to someone, the amount to be entered must be set to be greater than zero and not greater than the available funds. Length check - the input must not be too long or too short. For example, a surname will require at least one letter, but is unlikely to require more than 40. Presence check - a data value must be entered. For example, a quantity is required when placing an order. Format check - the data must be in the correct format. For example, a date must be in the format DD/MM/YYYY. Type check - the data must be of a specified data type, such as an integer when a quantity is to be specified.
136
Does validation ensure that the data is correct? | - how to get around this
Validation does not ensure that the data entered is correct, just that it is possible and sensible. A user may accidentally enter a date of birth that is possible and sensible, but incorrect. The program has no way of knowing that the date has been entered incorrectly. To get around this, many programs include verification checks - they repeat the entered data to the user and ask them to confirm if this data is correct. If the user confirms the data, the program then assumes it to be correct. This is an example of how planning the design of a program can help reduce errors.
137
In validation, what if data does not follow the rules?
Validation applies rules to inputted data. If the data does not follow the rules, it is rejected, reducing the risk that incorrectly input data may crash a program.
138
What is authentication?
Authentication is the process of a user confirming that they are who they say they are on a computer system. In a network, this is often done through inputting a username and password. For networks that require high levels of security, authentication may include other methods.
139
The factors of authentication can be broken down into three main groups:
something you are - username, bank account number, or anything that identifies the user uniquely something you know - password, pin, secret answer to a question something you have - swipe card, biometrics, any other physical identifying device
140
Syntax error
Error in a program resulting from code not following syntax rules governing how to write statements in a programming language.
141
Logic error
Error in a program which does not cause a program to crash but causes unexpected results.
142
Purpose of testing?
The purpose of testing is to help the programmer remove such bugs and to ensure that the program functions as intended. Testing requires a test plan. This is a list of all the tests that the programmer intends to use to ensure the program functions as intended.
143
What should test data cover
Test data is data that is used to test whether or not a program is functioning correctly. Ideally, test data should cover a range of possible and impossible inputs, each designed to prove a program works or to highlight any flaws.
144
3 types of test data
normal data - typical, sensible data that the program should accept and be able to process boundary data - valid data that falls at the boundary of any possible ranges, sometimes known as extreme data erroneous data - data that the program cannot process and should not accept
145
Tests are laid out in a testing table, which indicates:
the test number a description of what the test intends to check the test data being used the type of test - normal, boundary or erroneous expected outcome actual outcome
146
``` valid ← FALSE WHILE valid = FALSE OUTPUT "Enter a number from 1 to 10" number ← USERINPUT IF number < 1 OR number > 10 THEN OUTPUT “Number outside the range 1 to 10. Enter another number” ELSE valid ← TRUE ENDIF ENDWHILE OUTPUT ("Number entered is ", number) ``` This program could be tested using the following normal, boundary and erroneous data.
Test no. Description Test data Test type Expected Actual 1 Test that a possible number is accepted 5 Normal Data is accepted Data is accepted 2 Test the lower boundary 1 Boundary Data is accepted Data is accepted 3 Test the upper boundary 10 Boundary Data is accepted Data is accepted 4 Test that the program does not accept a number less than 1 -5 Erroneous Data is not accepted Data is not accepted 5 Test that the program does not accept a number greater than 10 20 Erroneous Data is not accepted Data is not accepted
147
Advantages of subroutines and the structured approach
``` Decomposition Abstraction Generalisation Re-usable Easier to read code Debugging Maintenance Development teams Local variables ``` Using subroutines means that a top-down design can be naturally achieved because the main program forms the top of the tree with each subroutine being a branch of the tree. This means that each task within the top-down design can be tackled separately rather than trying to tackle the whole problem at once. Abstraction hides unnecessary data. With subroutines, the detail of how each subroutine is removed from the main program meaning that only the identifier name of the subroutine needs to be used, plus any parameters. Subroutines can be used for a variety of different situations. This generalises the subroutine so it is not just used once. For example, a subroutine that finds the average of all values in an array can be used for any array and not just a specific array within a program. ``` Subroutines can be used over and over again within a program and if they are within a module they can even be used in other programs. This saves having to repeat sections of code as the subroutine just needs to be called each time instead. It is easier to read the name of a subroutine than all the detail involved in how the subroutine works. This makes the code easier to follow. ``` Each subroutine can be tested on its own without being executed as part of the main program. This means that any bugs within the subroutine can be corrected before it is tested within the main program. If code needs to be changed in the future, then it is easier to change the code within a subroutine rather than having to work through all the code. It is possible for different people to write the code for each subroutine because each subroutine can exist separately. All the advantages of local variables discussed on other flashcard