GCSE
COMPUTER
SCIENCE
PROGRAMMING
CONCEPTS
Suitable for
CIE Examboard
Name: ____________________________________
Class: ____________________________________
Classification: Internal
Variables and Constants
A named data store that contains a value
VARIABLE that may be changed during the running of
a program
Can only be used by the part of the
LOCAL VARIABLE
program it has been declared in
GLOBAL VARIABLE Can be used by any part of the program
A named data store that contains a value
CONSTANT that does not change during the execution
of a program
Determines what type of value the variable
DATA TYPE
will hold
ARITHMETIC OPERATORS Used to perform calculations
Used to decide which path to take through
LOGICAL OPERATORS
a program
Decides whether an expression is true or
BOOLEAN OPERATORS
false
DATA TYPE DESCRIPTION EXAMPLE
Char Single alphanumeric character A
String One or more alphanumeric characters Computer Science
Integer Whole number 35
Real Decimal number 1.4
Boolean True/False TRUE
ARITHMETIC OPERATOR DESCRIPTION
+ Add
- Subtract
* Multiply
/ Divide
^ Raise to the power of
1
Classification: Internal
LOGICAL OPERATOR DESCRIPTION
> Greater than
< Less than
= Equal to
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
BOOLEAN OPERATOR DESCRIPTION
AND Both true
OR Either true
NOT Not true
PSEUDOCODE VISUAL BASIC
DECLARE FirstVar : INTEGER Dim FirstVar as Integer
DECLARE SecondVar: STRING Dim SecondVar as String
CONSTANT FirstVar ← 500 Const FirstVar as Integer = 500
CONSTANT SecondVar ← “Variable” Const FirstVar as String = “Variable”
2
Classification: Internal
3
Classification: Internal
4
Classification: Internal
Library Routines
MOD Returns remainder of a division
Returns the quotient (i.e. The whole number
DIV
part) of a division
Returns a value rounded to a given number
ROUND
of decimal places
RANDOM Returns a random number.
PSEUDOCODE VISUAL BASIC
Value1 ← MOD(10,3) Value1 = 10 MOD 3
Value1 ← DIV(10,3) Value1 = 10 DIV 3
Value1 ← ROUND(6.9724,2) Value1 = Math.Round(6.9724,2)
Value1 ← RANDOM() Value1 = Rnd()
5
Classification: Internal
6
Classification: Internal
String Handling
LENGTH Find the number of characters in a string
SUBSTRING Extract part of a string
UPPER Convert all letters to uppercase
LOWER Convert all letters to lowercase
PSEUDOCODE VISUAL BASIC
LENGTH(“Computer Science”) “Computer Science”.length()
LENGTH(MyString) MyString.length()
SUBSTRING(“Computer Science”, 10, 7)
SUBSTRING(MyString, 10, 7)
“Computer Science”.Substring(10,7)
MyString.Substring(10,7)
Parameters: string, position of first
character, length of required substring
UCASE(“Computer Science”) UCase(“Computer Science”)
UCASE(MyString) UCase(MyString)
LCASE(“Computer Science”) LCase(“Computer Science”)
LCASE(MyString) LCase(MyString)
7
Classification: Internal
8
Classification: Internal
(b)
10
9
Classification: Internal
Input and Output
PSEUDOCODE VISUAL BASIC
INPUT first_name First_name = Console.readline()
OUTPUT “Hello” Console.writeline(“Hello”)
OUTPUT “Hello”, first_name Console.writeline(“Hello “ & first_name)
11
10
Classification: Internal
11
Classification: Internal
Arrays
A data structure containing several
elements of the same data type; these
elements can be accessed using the same
ARRAY
identifier name. The position of each
element in an array is identified using the
array’s index
PSEUDOCODE VISUAL BASIC
Declaring: Declaring:
DECLARE MyList: ARRAY[0:9] OF INTEGER Dim MyList(9) as Integer
DECLARE MyTable: ARRAY[0:9,0:2] OF Dim MyList(9,2) as Integer
INTEGER
Parameters: row, column Parameters: row, column
Storing: Storing:
MyList[4] ← 27 MyList(4) = 27
Populating using a loop:
Populating using a loop:
Console.writeline(“Enter these three values:
OUTPUT “Enter these three values: 1, 4, 89”
1, 4, 89”)
FOR Counter ← 0 TO 3
FOR Counter = 0 TO 3
OUTPUT “Enter next value”
Console.writeline(“Enter next value”)
INPUT MyList[Counter]
MyList[Counter] = console.readline()
NEXT Counter
NEXT Counter
12
12
Classification: Internal
13
14
13
Classification: Internal
Selection
CONDITIONAL STATEMENTS Used to decide what action should be taken
PSEUDOCODE VISUAL BASIC
IF Age > 17
IF Age > 17 Then
THEN
Console.writeline(“You are an adult”)
OUTPUT “You are an adult”
ELSE
ELSE
Console.writeline(“You are a child”)
OUTPUT “You are a child”
End If
ENDIF
15
16
14
Classification: Internal
17
15
Classification: Internal
16
Classification: Internal
Iteration
COUNT-CONTROLLED LOOPS Has a set number of repetitions
CONDITION-CONTROLLED LOOPS Will end once a condition has been met
PSEUDOCODE VISUAL BASIC
FOR i = 1 TO 10 For counter = 1 to 10
OUTPUT i Console.writeline(counter)
NEXT i Next
INPUT number Number = console.readline()
WHILE number != 0 WHILE number <> 0
OUTPUT number Console.writeline(number)
INPUT number Number = Console.readline()
END WHILE END WHILE
REPEAT Do
OUTPUT counter Console.writeline(counter)
Counter ← counter + 1 Counter = counter + 1
UNTIL counter = 5 Loop Until counter = 5
17
Classification: Internal
18
(b)
18
Classification: Internal
19
20
(a)
(b)
19
Classification: Internal
21
22
20
Classification: Internal
Totalling and Counting
TOTALLING Keeping a total that values are added to
Keeps count of the number of times an
COUNTING
action is performed
PSEUDOCODE VISUAL BASIC
Total ← Total + Mark Total = Total + Mark
Count ← count + 1 Count = count + 1
23
21
Classification: Internal
22
Classification: Internal
Maximum, Minimum, Average
MAXIMUM Finding the largest value
MINIMUM Finding the smallest value
AVERAGE Finding the average/mean value
PSEUDOCODE VISUAL BASIC
Maxmark ← 0
Dim Maxmark as Integer = 0
FOR Counter ← 1 to ClassSize
FOR counter = 1 To ClassSize
IF StudentMark[Counter] > Maxmark
If StudentMark[Counter] > Maxmark Then
THEN
Maxmark = StudentMark[Counter]
Maxmark ← StudentMark[Counter]
End If
ENDIF
NEXT Counter
NEXT Counter
Minmark 100 Dim Minmark as Integer = 100
FOR Counter ← 1 to ClassSize FOR counter = 1 To ClassSize
IF StudentMark[Counter] < Minmark If StudentMark[Counter] < Minmark Then
THEN Minmark = StudentMark[Counter]
Minmark ← StudentMark[Counter] End If
ENDIF NEXT Counter
FOR Counter ← 1 TO ClassSize FOR Counter = 1 To ClassSize
Total ← Total + StudentMark[Counter] Total = Total + StudentMark[Counter]
NEXT Counter NEXT Counter
Average ← Total/ClassSize Average = Total/ClassSize
24
23
Classification: Internal
(6 marks)
24
Classification: Internal
25
25
Classification: Internal
26
Classification: Internal
(15 marks)
27
Classification: Internal
Searching
Used to check if a value is stored in a list by
LINEAR SEARCH
working through the items in the list
PSEUDOCODE VISUAL BASIC
Console.writeline(‘Please enter a name to
OUTPUT “Please enter a name to find”
find”)
INPUT Name
Name = console.readline()
Found ← False
Found = False
Counter ← 1
Counter = 1
REPEAT
REPEAT
IF Name = StudentName[Counter]
IF Name = StudentName(Counter) Then
THEN
Found = True
Found ← True
Else
ELSE
Counter = Counter + 1
Counter ← Counter + 1
UNTIL Found > ClassSize OR Counter >
UNTIL Found OR Counter > ClassSize
ClassSize
26
28
Classification: Internal
Sorting
Ordering items in a list in a meaningful way
BUBBLE SORT
(e.g. alphabetically, ascending/descending)
PSEUDOCODE VISUAL BASIC
First ← 1 Dim First as Integer = 1
Last ← 100 Dim Last as Integer = 100
REPEAT Repeat
Swap ← False Swap = False
FOR Index ← First TO Last-1 For Index = First TO Last – 1
IF Temp[Index] > Num[Index + 1] IF Temp(Index) > Num(Index + 1)
THEN THEN
Temp ← Num[Index] Temp = Num(Index)
Num[Index] ← Num[Index + 1] Num(Index) = Num(Index + 1)
Nume[Index + 1] ← Temp Nume(Index + 1) = Temp
ENDIF ENDIF
NEXT Index NEXT Index
Last ← Last - 1 Last = Last - 1
UNTIL (Not Swap) OR Last = 1 UNTIL Swap = True OR Last = 1
27
29
Classification: Internal
Procedures and Functions
A set of programming statements grouped
together under a single name that can be
PROCEDURE
called to perform a task at any point in a
program.
Like a procedure but, in contrast, a function
FUNCTION will return a value back to the main
program.
The variables that store the values of the
PARAMETER arguments passed to a procedure or
function
PSEUDOCODE VISUAL BASIC
PROCEDURE Stars (Number : INTEGER) Sub Stars(Number As Integer)
DECLARE Counter : INTEGER Dim Counter As Integer
FOR Counter ← 1 TO NUMBER For Counter = 1 To Number
OUTPUT "*" Console.Write("*")
NEXT Counter Next
ENDPROCEDURE End Sub
To call a procedure: To call a procedure:
CALL Stars (7) Stars(7)
Or Or
MyNumber ← 7 MyNumber = 7
CALL stars (MyNumber) Stars(MyNumber)
FUNCTION CalculateArea(Length: INTEGER, Sub CalculateArea(length As Integer, width
Width: INTEGER) RETURNS INTEGER As Integer)
Area ← Length * Width Dim area As Integer = length * width
RETURN Area Return area
ENDFUNCTION End Sub
To call a function: To call a function:
OUTPUT CalculateArea(5,3) CalculateArea(5,3)
Or Or
ShapeArea ← CalculateArea(5,3) ShapeArea = CalculateArea(5,3)
30
Classification: Internal
28
29
30
31
Classification: Internal
31
32
Classification: Internal
File Handling
OPEN…FOR WRITE Opens a file for writing
WRITEFILE Writes a line of text to the file
OPEN…FOR READ Opens a file for reading
READFILE Reads a line of text from the file
CLOSEFILE Closes the file
PSEUDOCODE
DECLARE TextLine : STRING // variables are declared as normal
DECLARE MyFile : STRING
MyFile ← "MyText.txt"
// writing the line of text to the file
OPEN MyFile FOR WRITE // opens file for writing
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE, TextLine // writes a line of text to the file
CLOSEFILE(MyFile) // closes the file
// reading the line of text from the file
OUTPUT "The file contains this line of text:"
OPEN MyFile FOR READ // opens file for reading
READFILE, TextLine // reads a line of text from the file
OUTPUT TextLine
CLOSEFILE(MyFile) // closes the file
33
Classification: Internal
VISUAL BASIC
'writing to and reading from a text file
Imports System.IO
Module Module1
Sub Main()
Dim textLn As String
Dim objMyFileWrite As StreamWriter
Dim objMyFileRead As StreamReader
objMyFileWrite = New StreamWriter("textFile.txt")
Console.Write("Please enter a line of text ")
textLn = Console.ReadLine()
objMyFileWrite.WriteLine(textLn)
objMyFileWrite.Close()
Console.WriteLine("The line of text is ")
objMyFileRead = New StreamReader("textFile.txt")
textLn = objMyFileRead.ReadLine
Console.WriteLine(textLn)
objMyFileRead.Close()
Console.ReadLine()
End Sub
End Module
32
34
Classification: Internal
Maintaining a Program
Enables programmers to easily recognise
MEANINGFUL IDENTIFIERS
the purpose of a variable
Annotations on each section of a program
USE OF COMMENTS so programmers know what the purpose of
that section is and they can find it easily //
To make programs modular and easier to
PROCEDURES AND FUNCTIONS
update / add functionality
33
35
MARK
SCHEME
Classification: Internal
VARIABLES AND CONSTANTS
LIBRARY ROUTINES
5
Classification: Internal
STRING HANDLING
7
Classification: Internal
10
INPUT AND OUTPUT
11a
11b
ARRAYS
12
13
Classification: Internal
14a(i)
14a(ii)
SELECTION
15
16
Classification: Internal
17a
17b
17c
ITERATION
18a
18b
Classification: Internal
19
20a
20b
18b
21
22
Classification: Internal
TOTALLING AND COUNTING
23a
23b
MAXIMUM MINIMUM AND AVERAGE
24
Classification: Internal
25
25
Classification: Internal
25
SEARCHING
26
Classification: Internal
SORTING
27
PROCEDURES AND FUNCTIONS
28
29
30
31
Classification: Internal
FILE HANDLING
32
MAINTAINING A PROGRAM
33