
266
11 Programming
11
Table 11.1 shows the declaration of some of the constants and variables from
Example 11.1 in the three prescribed programming languages.
While the Cambridge International AS Level syllabus does not require you
to be able to write program code, the ability to do so will increase your
understanding, and will be particularly beneficial if you are studying the full
Cambridge International A Level course.
OUTPUT "Please enter the radius of the sphere "
INPUT radius
Check the value of the radius, to ensure that it is suitable.
WHILE radius <= 0 DO
OUTPUT "Please enter a positive number "
INPUT radius
ENDWHILE
Calculate the volume and the surface area; this is the processing part of the
algorithm.
volume ← (4 / 3) * pi * radius * radius * radius
surfaceArea ← 4 * pi * radius * radius
Finally, the results of the calculations need to be output.
OUTPUT "Volume is ", volume
OUTPUT "Surface area is ", surfaceArea
Declarations of constants and variables Language
pi = 3.142
Python does not require any separate
declarations and makes no difference
between constants and variables
Dim radius As Decimal
Dim volume As Decimal
Dim surfaceArea As Decimal
Const pi As Decimal = 3.142
Or
Dim radius, volume, surfaceArea As Decimal
Public Const pi As Decimal = 3.142
In VB, constants and variables are
declared before use. Declarations
can be single statements or can
contain multiple declarations in a
single statement. Constants can
be explicitly typed as shown or
implicitly typed, for example:
Const pi = 3.142
final double PI = 3.142;
:
:
double volume = (4 / 3) * PI * radius * radius * radius;
In Java, constant values are declared
as variables with a final value so no
changes can be made. These final
variable names are usually capitalised
to show they cannot be changed.
Variables are often declared as they
are used rather than at the start of
the code
▲ Table 11.1
457591_11_CI_AS & A_Level_CS_264-282.indd 266 25/04/19 11:21 AM