0% found this document useful (0 votes)
38 views14 pages

Chapter 8

Uploaded by

queensoldier101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views14 pages

Chapter 8

Uploaded by

queensoldier101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Science Chapter 8 - Programming M.

Muhi U Din

8.1 Programming concepts

8.1.1 Variables and constants


In a program, data can either have a fixed value (constant) or a value that may change (variable)
during execution.
Variables: Named data stores with values that can change during program execution. Variables
should have meaningful names to enhance code readability.
Constants: Named data stores with fixed values that remain unchanged during program
execution. Constants should also have meaningful names.
While some programming languages explicitly differentiate between variables and constants,
others do not. Programmers should clearly define which data stores are modifiable.
It is good practice to declare variables and constants in a program. Declarations may explicitly
define the data type or be implicit, where the type is inferred from the assigned value.
Declarations can occur at the program's start or before the first use of the data.
Sample declaration statements are typically provided in pseudocode for reference.

8.1.2 Basic data types


Data types are essential in computer systems to process and store data effectively, allowing:

 Appropriate storage, such as numbers or characters.


 Effective manipulation, like using mathematical operators for numbers or concatenation
for characters.
 Automatic validation in some cases.
Computer Science Chapter 8 - Programming M. Muhi U Din

The key data types for IGCSE Computer Science are:

 Integer: Positive or negative whole numbers used with mathematical operators.


 Real: Positive or negative numbers with fractional parts, also used with mathematical
operators.
 Char: A single character variable or constant.
 String: A sequence of characters (letters, digits, or symbols), which can vary in length or
be empty. Numbers stored as strings cannot be used in calculations.
 Boolean: A variable or constant with only two possible values: TRUE or FALSE
8.1.3 Input and output
To handle data effectively, programs use input and output statements. For IGCSE Computer
Science, students must write algorithms and programs that:
Take input from a keyboard.
Output results to a screen.
To ensure usability, input should include a prompt to guide the user on what data is expected.
Similarly, each output should be accompanied by a message explaining the result to make it
useful. When there are multiple parts in an output statement, they are separated by a separator
character.
Input data must match the data type of the variable where it is stored. By default, all inputs are
treated as strings. If the input needs to be an integer or real number, commands like int() or
float() are used to convert the data type (e.g., in Python).
8.1.4 Basic concepts
8.1.4 (a) Sequence
The ordering of the steps in an algorithm is very important.

Lets see an example of an incorrect pseudo code and its output compared with the correct one and its
output
Computer Science Chapter 8 - Programming M. Muhi U Din
Computer Science Chapter 8 - Programming M. Muhi U Din

8.1.4 (b) Selection


Selection is a very useful technique, allowing different routes. Data can be picked on a criteria using IF
and CASE statements.
Computer Science Chapter 8 - Programming M. Muhi U Din

8.1.4 (c) Iteration


There are three types of loops structures available to perform iterations.

 Count controlled loops


 Pre-condition loops – may have no iterations
 Post-condition loops – always has at least one iteration
Count controlled loops:
These types of loops are used when number of iterations are known.

Condition controlled loops:

 Pre-condition loop is run when the condition is met. (the condition is checked before)
 Post-condition loop is run first and then the condition is checked.
Computer Science Chapter 8 - Programming M. Muhi U Din

Practice question:
Write a program in which marks of ten students in an array of marks[] are checked. The marks
must be between 0 and 100, and if the marks are 0-49 then print “fail” else print “pass”

8.1.4 (d) Totalling and counting


8.1.4 (e) String Handling
8.1.4 (f) Arithmetic. Logical and Boolean operators

8.1.5 Use of nested statements


Selection and iteration statements can be nested one inside the other. This powerful method
reduces the amount of code that needs to be written.
Computer Science Chapter 8 - Programming M. Muhi U Din

8.1.6 Procedures and functions


Procedure is a set of programming statements grouped together under a single name that can
be called to perform a task at any point in a program.
Function is a set of programming statements grouped together under a single name that can be
called to perform a task at any point in a program but it returns a value back.
Parameter are the variables that store the values of the arguments passed to a procedure or
function. Some but not all procedures and functions will have parameters.
Computer Science Chapter 8 - Programming M. Muhi U Din

Function
Functions is like a procedure except it always returns a value.
The keyword RETURN is used as one of the statements in a function to specify the value to be
returned.

A function call must match the function definition. The arguments in the function call should
match the parameters in the procedure definition.
When procedure and functions are defined, the first statement in the definition is a header,
which contains:

 The name of the procedure or function


 Any parameters passed with data type
 The data type return value
Local and Global variables
A global variable can be used by any part of a program – its scope covers the whole program.
A local variable can only be used by the part of the program it has been declared in—its scope is
restricted to that part of the program.
Computer Science Chapter 8 - Programming M. Muhi U Din
Computer Science Chapter 8 - Programming M. Muhi U Din

8.1.7 Library routines


Programming language development systems include library routines. These routines are already
made by the development systems ready to use. A programming language IDE (integrated
development environment) have a standard library of functions and procedures.
Sometimes, a set of routines needs to be identified at the start of the program, which can give
access to the required routines.

 MOD – returns the remainder of a division


 DIV – returns quotient (whole number) of a division
 ROUND – returns a value rounded to a given number of decimal places
 RANDOM – returns a random number

8.1.8 Creating a maintainable program


Once a program is written, it may need to be maintained or updated by another programmer
later. The programmer mostly has a source code only.
Computer Science Chapter 8 - Programming M. Muhi U Din

A maintainable program should:

 Always use meaningful identifier names for:


o Variables
o Constants
o Arrays
o Procedures
o Functions
 Be divided into modules for each task using:
o Procedures
o Functions
 Be fully commented
(for example python uses # to start a comment for every line)

8.2 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 with position of each elements in an array
(index)

8.2.1 One and Two dimensional arrays


Lists can be stored in arrays, can be searched and put into an order. The first index can be 0 or 1.
Most programming language set the first index to zero.
Arrays can be multi-dimensional. We will study one and two-dimensional array

8.2.2 Declaring and populating arrays with iteration


One dimensional array
Computer Science Chapter 8 - Programming M. Muhi U Din

When a one-dimensional array is declared in pseudo code, followings are included:

 The name of the array


 First index
 Last index
 Data type

Two dimensional array


A two dimensional array can be referred to as a table (with rows and columns)
Computer Science Chapter 8 - Programming M. Muhi U Din

When a two dimensional array is declared in pseudo code, the followings are included:

 First index value for rows


 Last index value for rows
 First index value for columns
 Last index value for columns
 And the data type
Computer Science Chapter 8 - Programming M. Muhi U Din

8.3 File handling


8.3.1 Purpose of storing data in a file
Computer programs store data that will be required again, in a file. Any data stored in RAM will
be lost when the computer switches off; when data is stored in a file, it is stored permanently.
Data stored can be accessed again and again by a program or by another program later.

8.3.2 Using files


Every file has a name. You can read and write data on and from the file.

You might also like