Programming
Programming
Programming Languages
There are many high-level programming languages to choose from. We will only be treating
Python, Visual Basic, or Java.
Visual Basic is a popular programming language that is extensively used for Windows
development. The integrated development environment (IDE) featured in this chapter is
known as Visual Studio, which is utilised for capturing screenshots.
Programming Concepts
Constructs of a Program
Operators use arithmetic for calculations and logic and Boolean for decisions.
A variable within a computer program refers to a named storage unit with a value that can
be modified throughout the program's execution. To enhance comprehension for others, it is
advisable to assign significant names to variables.
A constant within a computer program represents a named storage unit that holds a value
which remains unchanged throughout the program's execution. Similar to variables, it is
recommended to assign meaningful names to constants to enhance comprehensibility for
others.
Data Types
Different data types are assigned to computer systems for effective processing and storage.
Data types enable effective manipulation using mathematical operators for numbers and
character concatenation.
In IGCSE Computer Science, algorithms and programs are designed to take input from a
keyboard and output to a screen.
Prompting the user with clear instructions for input is necessary for the user to understand
what is expected.
Input data in programming languages must match the required data type of the variable
where it will be stored.
By default, inputs are treated as strings, but commands can convert input to integer or real
number data types.
Users should be provided with information about the output/results for a program to be
useful.
If an output statement has multiple parts, they can be separated by a separator character.
Basic Concepts
When writing the steps required to solve a problem, the following concepts need to be used and
understood:
Sequence
Selection
Iteration
String handling
Use of operators.
Sequence
The ordering of the steps in an algorithm is very important. An incorrect order can lead to incorrect
results and/or extra steps that are not required by the task.
Selection
Selection is a very useful technique, allowing different routes through the steps of a program. The
code of this is explained in the notes of previous chapters.
Iteration
String Handling
Strings are used to store text and can contain various characters.
An empty string has no characters, while the programming language specifies the maximum
number of characters allowed.
Characters in a string can be identified by their position number, starting from either zero or
one, depending on the programming language.
In IGCSE Computer Science, you will need to write algorithms and programs for the following
string methods:
LENGTH("Text Here")
LENGTH(Variable)
// returns the next 7 values starting from the 10th value of the string "Computer Science" i.e.
"Science"
UCASE("Text here")
UCASE(Variable)
LCASE("Text Here")
LCASE(Variable)
Selection and iteration statements can be nested, meaning one statement can be placed
inside another.
Nested statements help reduce code duplication and simplify testing of programs.
Different types of constructs can be nested within each other, such as selection statements
within condition-controlled loops or loops within other loops.
Parameters refer to variables that store the values of arguments passed to a procedure or
function. While not all procedures and functions require parameters, some utilize them to
facilitate their operations.
PROCEDURE ProcedureName ()
[Commands]
ENDPROCEDURE
CALL ProcedureName()
[Commands]
ENDPROCEDURE
Function:
[Commands]
RETURN ValueToBeReturned
ENDFUNCTION
When defining procedures and functions, the header is the first statement in the definition.
The header includes:
o Parameters passed to the procedure or function, along with their data types.
Function calls are made as part of an expression, typically on the right-hand side.
Any part of a program can use a global variable – its scope covers the whole program
A local variable can only be used by the part of the program it is declared in – its scope is
restricted to that part of the program.
Note: Any variables/arrays made in this procedure and functions will be local and cannot be used
out of these. To be made available all over the program, they must be declared globally in the
following way.
Library Routines
Programming language development systems often provide library routines that can be
readily incorporated into programs.
Library routines are pre-tested and ready for use, making programming tasks easier.
DIV – returns the quotient (i.e. the whole number part) of a division
Examples:
be divided into modules for each task using procedures and functions
Commenting in pseudocode:
""
""
Arrays
An array is a data structure containing several elements of the same data type; these
elements can be accessed using the same identifier name.
The position of each element in an array is identified using the array’s index.
One-Dimensional Array
Two-Dimensional Array
When a two-dimensional
array is declared in pseudocode, the first and last index values for rows and the first and last
index values for columns alongside the data type are included.
Declaring a 2D Array:
FOR ColumnCounter ← 0 TO 2
FOR RowCounter ← 0 TO 9
NEXT RowCounter
NEXT ColumnCounter
File Handling
Data stored in RAM is volatile and will be lost when the computer is powered off.
Data saved to a file is stored permanently, allowing it to be accessed by the same program at
a later date or by other programs.
Key point: When writing in a file, the program is outputing the data to the file, and when reading a
file, the program in inputing the data from the file
\n There are 3 ways a file can be opened in a program i.e. to write, to read and to append
Writing in a file
//When opening a file to write, all the data already existing in the file is OVERWRITTEN
// The next command of WRITEFILE would be writen on next line of the file
CLOSEFILE "filename.txt"
Reading a file:
// The value in the line (which is identified by the number of times this is being run) is stored in the
variable
CLOSEFILE "filename.txt"
// here the line can be outputted or stored in an array. This process will repeat until every line
ENDWHILE