Cambridge IGCSE - O Level Computer Science Programming Book For Microsoft Visual Basic Sample
Cambridge IGCSE - O Level Computer Science Programming Book For Microsoft Visual Basic Sample
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Characteristics & classification of living organisms
LE
P
M
A
S
i
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
LE
P
M
A
S
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
Computer Science
LE
for Cambridge IGCSE™ & O Level
PROGRAMMING BOOK
FOR MICROSOFT® VISUAL BASIC
P Richard Morgan
M
A
S
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
LE
Cambridge University Press is part of the University of Cambridge.
It furthers the University’s mission by disseminating knowledge in the pursuit of education,
learning and research at the highest international levels of excellence.
www.cambridge.org
Information on this title: www.cambridge.org/9781108935678
© Cambridge University Press 2021
This publication is in copyright. Subject to statutory exception and to the provisions of relevant
collective licensing agreements, no reproduction of any part may take place without the written
permission of Cambridge University Press.
First published 2015
Second edition 2021
A catalogue record for this publication is available from the British Library
ISBN 978-1-108-93567-8 Programming Book Paperback with Digital Access (2 Years)
M
ISBN 978-1-108-94084-9 Digital Programming Book (2 Years)
Additional resources for this publication at www.cambridge.org/go
Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party internet
websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate
or appropriate. Information regarding prices, travel timetables, and other factual information given in this work is correct
at the time of first printing but Cambridge University Press does not guarantee the accuracy of such information thereafter.
Exam-style questions and sample answers have been written by the authors. In examinations, the way marks are awarded may be different.
A
References to assessment and/or assessment preparation are the publisher’s interpretation of the syllabus requirements and may not fully
reflect the approach of Cambridge Assessment International Education.
The information in Chapter 12 is based on the Cambridge IGCSE, IGCSE (9–1) and O Level Computer Science syllabuses (0478/0984/2210) for
examination from 2023. You should always refer to the appropriate syllabus document for the year of your examination to confirm the details
and for more information. The syllabus documents are available on the Cambridge International website at www.cambridgeinternational.org
S
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
LE
P
M
A
S
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Contents
The items in orange are available on the digital edition 3.5 Variable scope 42
that accompanies this book. 3.6 Arithmetic operators 43
3.7 More complex arithmetic operators 43
Introduction vi
3.8 Programming45
LE
How to use this book viii 3.9 Commenting code 48
How to use this series x 3.10 Making real-world
scenario-based systems 49
1 Visual Studio Community 3.11 Rounding function 59
1.1 Getting Visual Studio 2019 3.12 Random function 60
Community for Windows 2
4 Selection
1.2 The Integrated Development
Environment3 4.1 The need for selection 64
1.3 Getting started 4 4.2 IF statements 64
1.4 Console application 5 4.3 Single action IF statements 65
1.5
P
Make your first program using
console application 7
vi
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
Contents
LE
6.4 The complete design process 138 10.7 Two-dimensional arrays 217
10.8 Sorting224
7 Subroutines and file handling 10.9 Sorting algorithms 225
7.1 Why use subroutines? 141 10.10 Bubble sort 226
7.2 Subroutines in Visual Basic 142
7.3 Functions143
11 Manipulating strings
7.4 Procedures149 11.1 Concatenation235
7.5 File handling 155 11.2 Splitting strings 237
11.3 String index 237
8 Checking inputs 11.4 Substring241
8.1
8.2
8.3
9 Testing
Validation163
Verification164 P
Programming validation into
your systems 165
11.5 Looping through all the characters
in a string244
10 Arrays Acknowledgements271
10.1 What is an array? 201 Solutions
10.2 Declaring a one-dimensional array 201
S
vii
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Introduction
I have written this book with two aims in mind. The first is to provide a programming
book that specifically covers the material relevant to the Cambridge IGCSETM,
IGCSE (9–1) and O Level Computer Science syllabuses (0478/0984/2210). The second,
and perhaps more important, aim is to provide you with a start to the exciting and
rewarding process of being able to create your own computer programs.
LE
Language
The syntax and structures used to implement programming techniques will vary across
different languages. The book is entirely based around Visual Basic, one of the three
recommended languages for the syllabuses. Similar books are also available which
focus on the Python and Java programming languages.
Microsoft® Visual Basic offers you, as a programmer, two modes of application.
There is a simple console window in which you can learn and develop programming
skills. It also offers a Windows Forms application, which allows you to program
P
commercial-style applications that offer a graphical user interface through which
users can interact with programs. The language is supported by a fully functional
development environment called Visual Studio Community which is available free
directly from Microsoft. They also provide excellent support and language-specific
tutorials via the Microsoft Developer Network.
M
Support
As you work your way through the exercises in this book you will develop your
computational skills, independent of any specific programming language. You will
do this through the use of program design tools such as structure diagrams and
flowcharts. You will also make use of pseudocode, a structured method for describing
the logic of computer programs.
A
It is crucial that you become familiar with these techniques. Throughout this book all
the programming techniques are demonstrated in the non-language-specific format.
To support learning, all the chapters include examination-style tasks, both within
the chapter and as end-of-chapter tasks. Solutions to all the chapter tasks can be
found on the accompanying digital version of this book. There is also a chapter of
exam-style questions, which has a sample mark scheme prepared by the author, giving
S
possible solutions.
viii
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
Introduction
LE
prewritten code libraries, they are not generally used in this book.
Computational thinking is the ability to resolve a problem into its constituent parts
and to provide a logical and efficient coded solution. Experience of teaching GCSE
and A level computer science for more than 15 years tells me that knowing how to
think computationally relies much more on an understanding of the underlying
programming concepts than on the ability to learn a few shortcut library routines.
This book is aimed at teaching those underlying skills which can be applied to the
languages of the future.
It is without doubt that programming languages will develop over the coming years but
the ability to think computationally will remain a constant. As technology increasingly
P
impacts on society, people with computation thinking skills will be able to help shape
the way that technology impacts on our future.
M
A
S
ix
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LEARNING INTENTIONS
These set the scene for each chapter, help with navigation through the Visual Basic programming process and
LE
indicate the important concepts in each topic.
x
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
How to use this book
Programming tasks
Programming tasks give you the opportunity to develop your programming and
problem-solving skills. Answers to these questions can be found in the solutions
chapter, on the digital part of this resource. There are three different types of
programming tasks:
LE
DEMO TASKS
You will be presented with a task and a step-by-step solution will be
provided to help familiarise you with the techniques required.
PRACTICE TASKS
Questions provide opportunities for developing skills that you have learnt
about in the demo tasks.
CHALLENGE TASKS
P
Challenge tasks will stretch and challenge you even further.
M
SUMMARY
END-OF-CHAPTER TASKS
A
Questions at the end of each chapter provide more demanding programming tasks, some of which may require
use of knowledge from previous chapters. Answers to these questions can be found in the solutions chapter, on the
digital part of this resource.
NOTE: As there are some differences in the way programming statements are
S
structured between languages, you should always refer back to the syllabus pseudocode
guide to see how algorithms will be presented in your exam.
xi
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
uses relevant real-world contexts to bring topics to life, including two case
studies from Microsoft® Research. There is a skills focus feature containing
worked examples and questions to develop learners’ mathematical,
computational thinking and programming skills, as well as a programming
tasks feature to build learners’ problem-solving skills. The programming tasks
include ‘getting started’ skills development questions and ‘challenge’ tasks
to ensure support is provided for every learner. Questions and exam-style
questions in every chapter help learners to consolidate their understanding.
P
The digital teacher’s resource contains detailed guidance for all topics
of the syllabuses, including common misconceptions to elicit the areas
where learners might need extra support, as well as an engaging bank of
lesson ideas for each syllabus topic. Differentiation is emphasised with
advice for identification of different learner needs and suggestions of
M
appropriate interventions to support and stretch learners.
The digital teacher’s resource also contains scaffolded worksheets
for each chapter, as well as practice exam-style papers. Answers are
freely accessible to teachers on the ‘supporting resources’ area of the
Cambridge GO platform.
A
There are three programming books: one for each of the recommended
languages in the syllabuses – Python, Microsoft Visual Basic and Java.
Each of the books are made up of programming tasks that follow a scaffolded
S
xii
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
Chapter 1
Visual Studio
LE
Community P
M
A
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Introduction
The process of creating computer program code has changed considerably since the
early days of computer science, when the first programs were written directly in binary KEY WORD
using punch cards. Integrated
Today we use high-level programming languages. These languages use coding Development
statements that are close to human language. The code statements are then translated Environment (IDE):
to binary before being executed by the computer. The majority of high-level software that helps
programming is completed using an Integrated Development Environment (IDE), which programmers to
LE
provides the programmer with tools that help create, debug and translate the programs. design, create and
Visual Studio 2019 Community is an IDE that helps to generate programs written test program code.
using the Visual Basic programming language.
Visual Studio 2019 Community is the current version of free developer tools provided by Visual Studio
Microsoft. The IDE supports a wide range of programming languages including Visual 2019 Community:
a version of
P
Basic, Visual C++ and Visual C#. Visual Basic provides an interface that allows students
to develop programming skills while at the same time producing satisfying systems.
Visual Basic also provides programmers with access to a large class library. Classes
are templates that hold prewritten code that support functionality of objects. As your
skills increase, you will be able to use this feature-rich development environment to
produce and publish complex systems. System requirements and download options
the Integrated
Development
Environment (IDE)
produced by
Microsoft used to
create programs
M
can be found on the Microsoft website by searching ‘Visual Studio downloads’. using the Visual
Basic programming
language.
interface: the way in
which a user inputs
data into and receives
information from a
A
computer system.
classes: templates
that hold prewritten
code which supports
the functionality of
objects. All of the GUI
S
elements in Visual
Basic are objects of a
class. The GUI objects
can be created and
manipulated without
the need to write
code as the code
required is already
contained in the class.
2
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
LE
programming IDE. It offers many different languages and different formats. Community used
This is useful when developing for different environments such as web design, mobile to create programs
applications or computers. This book only uses two of the applications, console using the Visual
application and Windows Forms applications. Both make use of very similar coding Basic programming
approaches to algorithms but differ in how a user interacts with them. The features are language. This
shown in Table 1.1. interface most
directly matches the
Application Features of the application syllabuses.
Console application Used for creating a program that uses a Windows Forms
command-line interface on a computer running application: a
VB Console App a Windows operating system. This provides a GUI provided by
c:\
VB
(.NET Framework)
Graphical User
Because console application is the required programming format at this level, most of
Interface (GUI):
the code for the demonstration tasks use console application. But all the solutions to
an interface that
the practice tasks are shown in both applications.
includes graphical
elements, such as
windows, icons
and buttons.
3
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Option
P
Figure 1.1: Visual Studio 2019 home page
Description
Allows direct opening of programs used recently.
M
Alternatively, you can search for the recent
programs used.
4
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
The ‘Create a new project’ window provides the option to create any of the many
applications provided by the IDE. To reduce the available options, select Visual Basic
from the languages drop-down menu, shown in Figure 1.2.
LE
P
Figure 1.2: Create new project window
The two applications you will be using are shown in the list of available applications.
M
The applications may not appear at the top of the list as shown in Figure 1.2.
NOTE: Be careful when selecting the console applications. There is another console
application for use with macOS and Linux operating systems on the list. This is called
Console App (.NET Core)
You require Console App (.NET Framework)
A
5
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Figure 1.3: Configure your project window
P
The default layout of the console application consists of the main programming
window (see Figure 1.4). This provides an area in which you write all the program code
required to accept inputs, process data and display the required outputs. The Solution
Explorer on the top right-hand side of the window displays the contents of a solution.
A solution holds all the components of a project. Our projects will be limited to a
M
single module of code, which will be called Module1. You can see that shown in the
Solution Explorer as Module1.vb. If you accidently close your code window you can
reopen it by double clicking on Module1.vb in the Solution Explorer.
A
S
6
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
LE
console application
When the console application is first loaded the code window will contain four lines
of code.
Module Module1
Sub Main()
...
End Sub
End Module
KEY WORDS
P
Modules contain code and can hold a number of subroutines that perform specific
actions. You will learn more about subroutines in Chapter 7. Sub Main() is the
entry point for the program, and End Sub indicates the end of the subroutine.
Code written between these points will be executed when the application is run.
You can use the Enter key to add additional lines.
subroutine:
subroutines provide
an independent
section of code that
can be called from
M
another routine
TIP while the program
Do not change the name of the module Module1 or the subroutine Main( ). is running. In this
These are the module and subroutine that the project will run when it is way, subroutines can
executed. If the module or subroutine does not exist, the project will not run. be used to perform
common tasks within
a program.
Our first program will display the text ‘Hello World’. You need to code the application
execute: in
A
to display the required text. This is the code we will be using. The explanations are in
Computer Science,
the steps below.
the term ‘execute’
Module Module1 means the operation
Sub Main() of a computer
Console.WriteLine("Hello World") program. When a
Console.ReadKey() computer program
S
7
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Step 1a
Console.WriteLine("Hello World")
Type the word ‘Console’ into the Main subroutine shown in
the code window (see Figure 1.5).
As you type you will notice that the IDE provides an
autocompletion window listing all the code inputs or objects
that match the letters you have typed. To autocomplete
the entry, you can double click the correct item, or press
LE
the spacebar when the item is highlighted. This will speed
up your coding as once you have typed in the first few
characters of the instruction, the software will automatically
highlight the closest match.
P
Console class provides access to a library of methods that
allow the user to interact with the console.
When Console has been completed type a dot symbol. This
will show a list of all the available methods for the Console
class (see Figure 1.6). The method we need is ‘WriteLine’,
which will display text in the console window when the code
M
is executed. The method must be passed with the required
text: "Hello World".
NOTE: The text to be included is in quotation marks to
indicate that it is text and not a reference to another object.
Console.ReadKey()
The code Console.WriteLine("Hello World") will display the required text
in a console window when the application is run but the window will close as soon
as the code has been executed. To prevent this, use the console ‘ReadKey’ method to
pause the execution until a key is pressed on the keyboard.
S
8
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
TIP
Typing Imports System.Console at the start of the program means you
won’t have to type console when you use the class:
Imports System.Console
Module Module1
Sub Main()
WriteLine("Hello World")
ReadKey()
End Sub
LE
End Module
In this code, the Console class is imported at the beginning of the program.
This means that rather than having to write Console. … for both lines of
code in Sub Main(), you can just write the methods needed (in this case,
WriteLine("Hello World") and ReadKey()).
Step 3
To run the code, click the Start option on the toolbar Start or use F5 from the
P
keyboard. This will launch the console window (Figure 1.7) and display the text
‘Hello World’. The execution of the code will be halted until a key is pressed, at which
time the window will close.
M
A
While the console window is open, you may notice the Diagnostic Tools
running in the background (see Figure 1.8). This provides information about
the memory and CPU usage of the program being executed. While this is
helpful when designing commercial projects, it is outside the scope of the
S
9
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Figure 1.8: Diagnostic tools window
that can be selected by a user. When the button is selected, it will trigger the execution
of code held in a subroutine that is linked to the button. As you develop more complex
projects your GUI will include many different buttons that a user can select, each
button triggering a different section of code held in a subroutine linked to that button.
For example, a reset button would be linked to a subroutine that holds the code
required to reset the project.
S
In this first Windows Forms application, we will have a single button. Clicking this
button on the form will trigger the code in the subroutine linked to the button that will
deliver the message ‘Hello World’.
To create a Windows Forms application, you will need to first create a new project
(Figure 1.2) and select Windows Forms App (.NET Framework). You should give
your project a meaningful name and select an appropriate storage location when you
configure your project (Figure 1.3).
The default layout of a Windows Forms application contains five main areas, these are
shown in Figure 1.9 and described in Table 1.3.
10
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
LE
P
M
Toolbox Error List window Properties window
If your Windows Forms application interface does not include all of these windows,
or if you accidentally close a window, you can open additional windows by using the TIP
View option on the top ribbon menu. Individual windows
A
An alternative to opening individual windows via the View option is to return to the on the interface can
default layout. This can be accessed via the Window option on the top ribbon menu be sized, docked
(see Figure 1.10). or set as floating.
You can personalise
the display of the
interface to suit your
own preferences. If
S
11
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
It is likely that when you first access the interface the Toolbox will be minimised. If the
toolbox is minimised, it can be seen on the left-hand side of the interface (indicated by
the red circle in Figure 1.10).
When designing, a good idea is to pin the toolbox menu so that it remains open on
the interface. If you do not do this, each time you move onto another element of the
interface, the toolbox will minimise. This can be achieved by selecting the pin icon
(indicated by the red circle in Figure 1.11).
LE
Figure 1.11: The Toolbox pin icon
The toolbox contains nine different folders of available tools. If you open the
All Windows Forms folder you will see all the available options.
It is unlikely that you will need to use any GUI elements beyond those held in the
Common Controls folder. For our first form we will be using the Button and Textbox
P
elements (indicated by red ovals, Figure 1.12).
M
A
Window Description
Design and Provides an area in which you design the interface for your system and write the program
programming code required to accept inputs, process data and produce the required outputs.
Toolbox Provides a set of tools that will allow you to use a predefined visual GUI and control
objects for the application you wish to develop. You can use your mouse to drag and
drop any of the GUI options in the toolbox onto the form.
(continued)
12
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
Window Description
Properties Used to view and edit the configuration of selected objects. The Properties window shows
the current properties of the current GUI object selected (for example, font type and
size, colour, etc.). Changes to the properties of the object can be made by changing the
options in the properties window.
The properties of each element shown are those that are set before the program is
executed. Any changes made during the execution of the program will be lost when the
program is exited. For example, if a you wrote code to change the colour of a button
when a user clicked on it, the button would return to the colour shown in the Properties
LE
window when the program was restarted.
NOTE: The name of the GUI object currently selected is shown at the top of the window.
Ensure you have selected the required object before making changes.
Solution Explorer Displays the contents of a solution, which includes the forms and other project items. This
is where you will find your forms and the program files that support them. It is possible
to have multiple forms within one project; Solution Explorer will allow you to select
individual forms.
Error List Displays any errors, warnings or messages produced as you edit and execute code.
Table 1.3: Description of each window in the Windows Forms application programming interface
P
1.7 Make your first Windows Forms application
In a Windows Forms application, the ‘Hello World’ program will be achieved in
two steps:
M
1 design and construct the user interface
2 code the program that will generate the required output.
NOTE: The code window is activated by double clicking on the form itself or any GUI
object on the form. Soon we will deliberately activate the code window, but if you do
this by accident, you can easily navigate back to the forms interface.
A
S
The code tab Form1.vb and design tab Form1.vb (Design) are both shown in the
design and programming window (Figure 1.13), and selecting tabs allow you to swap
between views.
13
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
the button.
Use the same process to generate a textbox object abl TextBox on the form.
The form will initially look like Figure 1.14. We will customise this by changing the
properties of the individual elements.
P
M
Figure 1.14: Initial form design
interface to change the properties of each object. As you select an object on the form,
or the actual body of the form itself, the Properties window will change to reflect the
properties of the object or form. Objects have many properties that can be configured
by the designer, but we are initially only interested in the properties in Table 1.4.
Use these properties to personalise the button and textbox.
S
TIP
When giving objects identifiers (names), it is good practice to start the
identifier with capital letters that describe the type of object followed by a
meaningful name. For example, BTNShow for the button used to show the
‘Hello World’ message. Putting the type description at the start will mean that
when you type BTN on the code interface, the autocomplete will show all the
buttons on your form. You could also start the identifiers of all textboxes with
the letters TB followed by a meaningful name.
14
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
Property Description
The identifier (name) that
will be used to refer to
the object.
The font style that will used
to display text on the object.
LE
The text (currently Button1)
that will appear on
the object.
P
To change the properties of individual elements you must select the required element.
You will know which element you have selected because the name will be shown at the
top of the properties window (Figure 1.15).
M
Figure 1.15: Properties identifier
NOTE: If you accidently double click on an element you will activate the code window.
You can easily navigate back to the design window (see Figure 1.13).
A
The textbox name has been changed to TBMessage from the default value of
TextBox1. The font size of the textbox has also been increased to 12 pt.
15
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Figure 1.16: Customised form
The background colour of the form has been changed to green using the Custom TIP
colour pallet (Figure 1.17).
Make sure you
have selected the
correct element by
checking the name
at the top of the
properties window.
If you do not it is
possible to change
P
Figure 1.17: The BackColor property custom colour pallet
To open the code window, select Code from the View menu (Figure 1.18). The code
window will open as an additional tab in the main design window. Clicking the tabs
will switch between design and code windows (Figure 1.19).
S
16
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
The code window will contain only two lines of code. However, this code is important
as it indicates the start and end of the code attached to the form. All additional code TIP
will be placed between these two indicators. Generally, code placed outside will not be Double clicking
part of the form and will cause an error. The Enter key can make additional lines. an object on the
In Visual Basic, user-controlled events – such as pressing the Enter key or selecting a form to generate a
button on a GUI – control the execution of the code. This is known as event driven subroutine can be
programming. A common element of a Windows Forms application GUI is a button done by mistake. This
which can be selected by a user. When the button is selected it will trigger the execution could mean that if
of code held in a subroutine that is linked to the button. you have accidentally
double clicked on an
LE
In this first program, clicking the button on the form will trigger the execution of code object, your program
held in a subroutine linked to the button that will deliver the message ‘Hello World’. will contain unused
Before writing code, the event subroutine linked to the button has to be created. event subroutines. It
The simplest way to create a subroutine is to double click the object on the interface you is a common mistake
wish to use as a trigger (in this case, the button on the form). This will automatically create to place the correct
the code for the default subroutine attached to the object selected and show the code code in the wrong
window. event subroutine. If
your code does not
Public Class Form1
run as expected,
P rivate Sub BTNShow_Click(sender As Object,
check you have used
e As EventArgs) Handles BTNShow.Click
the correct event
End Sub
subroutine.
End Class
P
Let us examine the code to identify what each element achieves (Table 1.5).
17
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
We can now start to write some code. Within the Button Click subroutine, type in the
identifier (name) of your textbox. In this example, the identifier of the textbox has
been changed to TBMessage. As you type, you will notice that Visual Basic provides an
autocompletion window (Figure 1.20) listing all the code inputs or objects that match
the letters you have typed. You can double click the correct item or press the spacebar,
to autocomplete the entry.
LE
Figure 1.20: Autocomplete window
When the name of the textbox has been entered, type a dot symbol.
P
As the textbox is an object of the Textbox class, this will show a list of all the available
methods that can be attached to a textbox object. The method we need is the Text method,
which will either set text into a textbox or get text from a textbox. Fully type the word text,
or alternatively double click or press the spacebar to select the method (Figure 1.21).
M
A
To indicate the actual text that should be displayed complete the code:
Public Class Form1
Private Sub BTNShow_Click(sender As Object,
e As EventArgs) Handles BTNShow.Click
TBMessage.Text = "Hello World"
End Sub
End Class
NOTE: The text to be included is in speech marks to indicate that it is new text and not
a reference to another object.
18
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
1 Visual Studio Community
LE
Figure 1.22: Hello World
To run the code, click the Start option on the toolbar Start or select F5 from the
keyboard. This will launch the form as a separate interactive window. Click on the
button and the text ‘Hello World’ will appear in the textbox (Figure 1.22).
In Figure 1.22, the background colour of the form has been changed. Also, the text on
the button has been changed to read ‘Show Message’ and the font of both the button
and textbox to be larger than the default 8.25 pt.
Your first Windows Forms application program is complete. You have made use of
the design window and the Toolbox to create the interface. You have generated a
subroutine called by the Click method of a Button object. Within that subroutine you
A
have used the Text method of a Textbox object to place programmer-defined text into
the textbox.
Use the Save All option under the FILE menu to save your project.
19
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Windows Forms applications will offer a richer visual experience and produce systems
similar to those commercially available.
Making use of both applications will best support the development of your
computational thinking.
LE
language-specific formats included in the syllabuses. Visual Basic is used to provide
the opportunity for you to use a real programming language to develop your
understanding of these concepts. Additional support and guidance on the Visual Basic
programming language and Visual Studio 2019 Community can be accessed directly
from the Microsoft Virtual Academy.
A range of video tutorials and links to other support can be accessed from Microsoft’s
Visual Studio website. Just search for ‘Microsoft Visual Studio getting started’.
SUMMARY
P
Visual Studio 2019 Community is the latest version of the integrated development environment IDE) produced
by Microsoft. The IDE provides a set of tools that are used to design, create and debug programs using the
Visual Basic programming language.
Visual Basic makes use of classes which hold prewritten code that supports the functionality of elements
M
used. For example, the console class allows data to be read using console.readline() or displayed using
console.writeline. All of the GUI elements in Visual Basic are objects of a class. The GUI objects can
be created and manipulated without the need to write code as the code required is already contained in the class.
The Visual Studio 2019 Community IDE allows for programs to be created using either a console application
or a Windows Forms application.
• Console application is a text-based command-line interface, that most directly matches the syllabuses.
The code is produced using a single programming interface. When the code is executed the user-interface
A
is a single window used to input data into a system and output information from that system.
User interaction with the program is text based.
• Windows Forms application is a rich, event-driven programming environment that supports a graphical
user interface (GUI). The design of a program requires the development of the GUI. Elements of the
GUI are linked to subroutines in which code is written to perform the required tasks. When the program
is executed the user interacts through the use of elements such as buttons and text buttons on the GUI.
Information output from the system is also shown on the GUI. The GUI can be customised by the
S
20
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
Chapter 2
Programming
LE
constructs P
M
A
S
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Introduction
Many sporting activities make use of a limited range of approaches. Racket sports –
KEY WORD
such as badminton, for example – consist of a surprisingly limited number of different
shots. Drop shots close to the net, long cleared shots to the back of the court and hard- construct: a method
driven shots account for a large proportion of the shots used in a game. The skill is in of controlling the
combining those shots effectively to win the game. order in which
Programming also makes use of a limited range of constructs to achieve satisfactory the statements in
solutions to complex tasks. Like a good badminton player, who combines different an algorithm are
executed.
LE
shots to win a game, an effective programmer will combine different constructs to
produce efficient programs.
Iteration
P
different path through the program based on certain criteria.
Repeating a sequence of steps during the execution of the code.
Table 2.1: Programming concepts
In Computer Science, the term ‘execute’ means the operation of a computer program.
selection: code
follows a different
sequence based on
what condition is
chosen.
M
iteration: code
When a computer program is operating, it is being executed. The term ‘run’ is also
repeats a certain
used to describe the same process. The ‘program is running’ or the ‘program is being
sequence a number
executed’ both mean the same thing.
of times depending
on certain conditions.
Sequence
The order in which a process is completed is often crucial to the success of that process.
TIP
A
calculate the distance to be travelled as, without this data, you could not complete the
rest of the task. used in programming
in the same way it
Most tasks will involve different steps, and those steps often have to be completed applies to normal
in some logical sequence. When designing computer programs to achieve tasks, it is mathematical
crucial to consider the sequence in which the task needs to be completed. calculations.
22
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
LE
Obtaining the journey start and end points is crucial because without this information,
the rest of the process could not be completed. However, the order in which the two
locations is obtained is not that important. Had Step 1 and Step 2 been reversed,
the calculation at Step 3 could still have taken place.
In programming, the sequence is indicated by the order in which the code is written,
usually top to bottom. The program will execute the first line of code before moving to
the second and subsequent lines. An example of a sequence error would be completing
a process before all the appropriate user inputs had been obtained. For example,
trying to calculate the area of a rectangle when you only had the length of one side.
Selection
P
Often your programs will perform different processes dependent on user input.
Consider a system designed to provide access to the school network when a user inputs
a username and password. The system would need to follow different paths depending
on whether the user input was accurate or not.
M
One path would be followed if the username and password input matched records that
would allow network access. If the username and password input did not match the
records, the system would follow another path in which the network access would be
denied and the user prompted to re-input the details.
Figure 2.1: Different paths needed on input of username to access school network
Iteration
S
23
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
computer program
The next stage is to design an algorithm for the individual problems. Two approaches by breaking down
(shown in Table 2.2) that can be used at this stage to help create logically accurate the problem into
systems are flowcharts and pseudocode. smaller problems
(subsystems) until it
Flowcharts A graphical representation of the intended logic and sequence is sufficiently defined
of a program or ‘flow of a program’ (which is where the name to allow it to be
‘flowchart’ came from). It is a diagram that describes the flow that understood and
the execution of code will take through a program. programmed.
Pseudocode A method of describing the logic and sequence of a program using
algorithm: a process
natural language to explain each step in the intended process. The
or set of rules to
logic can be understood by programmers without the need for
P
detailed knowledge of any specific programming language. The
term ‘pseudo’ is derived from the early Greek word for ‘false’.
Table 2.2: Algorithm design tools
For this course, you will be expected to have a working understanding of both flowcharts
be followed during
the execution of a
program.
M
and pseudocode and to use them to answer questions that ask you to explain the logic of
your solutions to given tasks. Both methods are used throughout this book to indicate
the logic of systems, and it is important that you become familiar with their use.
KEY WORD
flowchart:
2.3 Flowcharts a graphical
representation of the
Flowcharts are graphical representations of the logic of the intended system. They make sequence and logic
A
use of symbols to represent operations or processes which are joined by lines that indicate of a program.
the sequence of operations. Table 2.3 details the symbols used.
START STOP
24
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
LE
Average’ would be too generic. It needs to
indicate the inputs or other values used to
calculate the average.
Logic flow Joins two operations.
INPUT OUTPUT
The arrowhead indicates the direction of A, B
result ←A * B
result
the flow.
P
determined is written within the symbol.
Where multiple alternatives exist, sequence
flows are indicated by chained decision
symbols. Each ‘FALSE’ condition directs to
another decision.
> 10
FALSE
FALSE
right(90) Is Input
Is Input
=C
=B
TRUE
TRUE
M
Table 2.3: Symbols used in a flowchart
25
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
CONTINUED
START
LE
Obtain Journey
Distance
Calculate
Journey Time
STOP
S
Flowchart 2.1: Flowchart for calculating arrival time for an aeroplane journey
This task follows a simple linear process, so the flowchart that represents the
process also follows a single path.
NOTE: Observe the use of the different symbols to indicate the inputs /
outputs from the processes that need to be completed. When you come to
code programs these will help to identify the need to obtain user input.
26
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
Flowchart design
A flowchart is often created in two stages.
Stage 1
Plan the logic and order of the system. The flowchart will show the intended sequence.
It will include the inputs, outputs, decisions and processes required. At this stage, the
process rectangle and decision diamonds are likely to include a text description of the
processes or decision that must be made. There may be little, if any, detail provided
about how the decisions or processes will be completed. This is known as high-level
LE
design and allows the programmer to check the logic of the system.
Stage 2
The decisions and processes will be added to provide details about how they will be
completed. As the design develops, you will be able to consider the factors involved in
each process and include a greater level of detail about each process. At this stage, a
programmer may discover that they need additional inputs to be able to complete the
process effectively. Wherever possible the final flowchart should include well-defined
processes. Effective flowcharts avoid ambiguity.
The arrival time flowchart (Flowchart 2.1) includes examples of both Stage 1 high-level
process and Stage 2 well-defined processes. Details of these processes are shown in
Table 2.4.
Stage
Stage 1
High-level
process
Process P
Calculate time to complete journey
While this description accurately describes the process, it does not
define how to complete the process. It does not define what speed
M
should be used to calculate journey time. Different aircraft fly at
different speeds, so it is likely that the user would be expected to
input the speed to be used. However, this step is not shown.
Stage 2 Arrival Time ← Start Time + Journey Time
Well-defined
process This is a well-defined process. There is little ambiguity about how
to complete the process.
The ← symbol means ‘becomes the result of’. In this example,
A
the arrival time will be the result of adding the journey time to the
start time of the flight.
Table 2.4: Examples of the two stages of flowchart creation
S
27
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Process A description of the process must be included in the
process symbol.
Complex processes should be broken down into a
series of simple steps, each step being represented by a
different processes symbol.
A process can have only a single outward flow. If a
process had two outward flow lines, which one would
be followed?
Decision A decision will have two outward flows. Where more
than one option exists, a sequence of decisions should
be used.
P
The condition that will control the flow of execution must
be included in the decision symbol.
It is crucial to label the outgoing flows TRUE and FALSE.
If these are missing the direction of flow is not known.
M
Table 2.5: Best practice in symbol detail
maximum possible score in the examination and the score achieved by the
candidate. Candidates who achieve 80% or more of the maximum score will
be invited for an interview to decide if they will be offered a place at the
university. Candidates who score less than 80% will be rejected.
28
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
CONTINUED
Solution
START
LE
INPUT Max Score
INPUT Candidate Score
Percent ←
Candidate Score / Max
Score * 100
FALSE TRUE
NOTE: Observe the use of the diamond symbol to indicate a decision. The
A
criteria are Boolean expressions that will either be TRUE or FALSE. It is crucial
that the TRUE/ FALSE indicators are added to show how the execution of the
code will flow.
29
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Solution
LE
START
INPUT Password1
INPUT Password2
FALSE
OUTPUT "Passwords do not Password1 =
match, please re-enter"
P Password2
TRUE
M
Accept New
Password
STOP
NOTE: Observe the return flow from the FALSE path of the decision symbol.
A flow that returns to an earlier stage in the flowchart represents iteration.
In this example, the return loop indicates the continual repetition of the steps:
• input password twice
• check passwords match
• if no match prompt for re-entry of passwords.
30
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
LE
You only have to include (Stage 1) general descriptions for this flowchart.
You do not yet know how to program the checks required, so a general
description of each check will be acceptable.
P
output the result of adding the two numbers together.
Consider the attempt at creating a flowchart for the task shown in Flowchart 2.4.
START
M
INPUT
Numbers
OUTPUT
Check in Range
"Not in range
A
10 to 20
re-enter"
INPUT
Calculate Result
Numbers
S
OUTPUT
STOP
31
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
CONTINUED
The flowchart includes three errors as well as elements that could be better defined.
Before you read on to the next part, which shows the errors and improvements,
see if you can:
1 spot the errors and explain how to correct them
2 suggest the possible improvements.
LE
While the flowchart may at first glance appear logical it does contain several errors
and ambiguities. Table 2.6 identifies and explains the errors in the flowchart and
Table 2.7 offers some improvements.
This is a process symbol and can have only one outward flow. This
Check in Range symbol should be replaced with one or more decision symbols.
10 to 20
Not only is the wrong symbol used, there are no TRUE or FALSE
options to indicate the direction of flow.
OUTPUT This attempt at a loop will cause the user to enter new numbers.
Check in Range
"Not in range However, it will only check the numbers once. If the user inputs
10 to 20
Calculate Result
re-enter"
INPUT
Numbers
P incorrect numbers a second time, the numbers will not be checked.
The task stated ‘Once both numbers are within the range’, so this does
not achieve the task requirements .
M
This is the correct symbol but does not show what is being output.
OUTPUT It should show OUTPUT Result.
INPUT Numbers It would be improved by indicating the two numbers as individual inputs. Either by
using two input symbols or by writing two INPUT descriptions in one input symbol.
Although this process is accurately described, it is not well defined. It does not
Calculate Result show what calculation should take place.
S
When replacing this incorrect symbol with decision symbols, the level of detail
Check in Range should be considered. Currently there is ambiguity in the words used. It does not
10 to 20 define whether the check should include the values 10 and 20 or whether they
should be excluded from the range.
This can be made clearer through using the mathematical > and < symbols.
Table 2.7: Improvements
32
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
CONTINUED TIP
The corrected flowchart now looks like Flowchart 2.5. When writing the detail in
the decision symbols, make
sure you include precisely
START
defined mathematical
conditions. These should be
defined using appropriate
mathematical symbols such
as > or <. Using terms such
LE
as ‘greater than’ or ‘less
INPUT Number1
than’ should be avoided.
INPUT Number2
FALSE
OUTPUT
"Not in range
P FALSE FALSE FALSE
M
re-enter"
Result ←
Number1 + Number2
OUTPUT
A
Result
STOP
S
The INPUT symbol now shows that two numbers need to be input.
The incorrect process ‘Check in Range 10 to 20’ has been replaced with a series of
decision symbols. Each decision defines the range being checked precisely.
33
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
CONTINUED
Any incorrect entry is shown the error message and redirected back to the INPUT
symbol. This process will continue to repeat until two in range values are input.
The result process shows the exact process to be completed and the final OUTPUT
symbol defines the value to be output.
Questions
A scenario question is likely to use words to describe acceptable range values.
LE
You will be expected to use precise mathematical terms to define the
range in a flowchart.
For the following scenarios, create an appropriate flowchart. Consider what inputs
are required and what values need to be checked. Remember to be precise when
defining the limits.
1 A roller-coaster has height and weight restrictions. To be able to ride the
roller-coaster people must be at least 1.10 metres tall. The maximum weight a
seat on the roller-coaster can safely hold is 125 kilograms. Only people who are
within these limits will be permitted to rider the roller-coaster.
2 A firm makes metal beams. Beams come in two strengths ‘standard’ and
3
P
‘extra strong’. The minimum length of beam they will make is 1 metre.
A standard beam can be no longer than 4 metres, but an extra strong beam can
be made in lengths up to 6.5 metres. Orders for beams outside of these limits
will be rejected. All other orders proceed to a process called ‘Calculate Price’.
A climate control system in a hospital operating theatre is required to maintain
a constant temperature of 21° Celsius. A temperature sensor inputs the actual
M
temperature in the operating theatre into the system. If the actual temperature
drops below 20° Celsius, the heating must be on. If the actual temperature rises
to 22° Celsius or more, the cooling system must be on. When the actual
temperature is between these two limits both the heating and cooling system
are off. Remember the temperature is constantly being checked.
A
logic of a program
Pseudocode can be understood by any programmer. This means that the final program
using both natural
can be coded using any programming language that is appropriate to the context.
language and
This is useful when designing programs as the code required to perform a particular code-like statements.
process can vary considerably across differing programming languages. It also means
that if there are multiple people working on a project, programmers from different
programming language backgrounds can discuss and develop ideas and logic using a
format common to them all.
34
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
LE
language is structured in a similar way to natural language and therefore closely
resembles pseudocode. Visual Basic also automatically indents instructions where
appropriate similar to the approach that should be adopted when writing pseudocode.
Pseudocode example
This pseudocode is for an algorithm that accepts the input of two numbers.
These values are added together and the result is stored in a memory area called Answer.
The value in Answer is then displayed to the user. (In Chapter 3, you will learn that this
memory area is known as a variable.)
// Entering the values
INPUT Number1
INPUT Number2
P
// Calculate the addition and store in Answer
Answer Number1 + Number2
// Output the value in Answer
OUTPUT Answer
M
NOTE: Observe the use of ← to show the passing of values. This is distinct from the
use of the equals symbol (=), which is used to indicate a comparison of two values.
(Visual Basic does not have the ← symbol and uses the = symbol in both situations.)
in programming
Because of the universal nature of flowcharts and pseudocode, they are used
extensively in the design of computer programs. The aim of this book is to help you
to learn to design effective systems using the programming language Visual Basic.
The following chapters make use of flowcharts and pseudocode to define the logic of
S
35
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
CAMBRIDGE IGCSE™ & O LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
LE
Grade as Pass if percentage in range 40–60
Grade as Fail if percentage less than 40.
Design a flowchart to show the logic of the system required to output the
correct grade.
2.2 A program is needed to work as a calculator. The user is asked to input
two separate numbers and an arithmetic operation (+ , ×, ÷ or −).
The program must then calculate the answer. This answer must be output.
Design a flowchart to show the logic of the system required to complete
this task.
SUMMARY
P
Programmers make use of three constructs when writing code:
M
• sequence: the logical order in which the code is executed
• selection: branching of code onto different paths based on certain conditions
• iteration: repeating of sections of code.
Before coding a program it is crucial to design an appropriate algorithm using either a flowchart or pseudocode.
Flowcharts use a series of symbols to visually represent the logic of a program. The flow of the execution of the
program is represented by flow lines with arrow heads to show the direction of flow. It is crucial that the correct
A
symbols are used. The examination board will provide a flowchart guide and you must become familiar with the
expected symbols.
Flowchart design can be done in two stages:
• the first stage is to consider the logic steps and processes required for the algorithm
• the next step is to consider how those steps could be achieved and ensure the flowchart contains enough
S
36
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.
We are working with Cambridge Assessment International Education towards endorsement of this title.
Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s).
2 Programming constructs
END-OF-CHAPTER TASKS
1 State the definitions of the following terms:
• an algorithm
• sequence
• selection
• iteration.
2 Explain what the following flowchart symbols are used for:
LE
3 Create a flowchart to show the logic of the algorithm for this system:
A user is required to input two numbers into the system. The second number must be exactly double the
first number.
The system will check that the second number is double the first. If this is not true, the system will output a
message and the user must re-enter the numbers. This process will continue until the second number is exactly
double the first.
P
When this happens, the system will output the addition of the two numbers.
4 A business supplies sand to building sites. The delivery truck used has a maximum weight limit of 12 tons so
no order greater than 12 tons can be accepted.
Sand is provided in two different qualities:
M
Soft Sand Charged $40.00 per ton (or part of a ton)
Sharp Sand Charged at $36.50 per ton (or part of a ton)
37
Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.