Programming Variables Constants Arrays Intro
Programming Variables Constants Arrays Intro
Chapter Outline:
Programming Concepts:
Arrays including:
▪ one- and two-dimensional arrays,
▪ use of indexes,
▪ use of iteration for reading from and writing to arrays
Learning Objectives:
✓ declare, use and identify appropriate data types for variables and constants
✓ understand and use input and outputs
✓ understand and use the programming concepts:
o sequence,
o selection,
o iteration,
o totalling,
o counting and
o string handling
✓ understand and use nested statements
✓ understand and use arithmetic, logical and Boolean operators
✓ understand and use procedures, functions and library routines
✓ write maintainable programs
✓ declare, use, identify appropriate data types for, read and write arrays with one and two dimensions
✓ store data in a file and retrieve data from a file.
PROGRAMMING CONCEPTS
There are five basic constructs to use and Rules for naming variables:
understand when developing a program: ✓ Variables should be named using meaningful
names that describe the purpose of the variable.
✓ data use – variables, constants and arrays
For example, a variable used to store the user's
✓ sequence – order of steps in a task
name could be named "userName".
✓ selection – choosing a path through a
userName = “John Doe” (create a variable
program
userName and stores a string value John Doe in
✓ iteration – repetition of a sequence of steps
it).
in a program
✓ Variables names can only contain letters (A-Z, a-
✓ operator use – arithmetic for calculations,
z), digits (0-9) and the underscore character (_).
logical and Boolean for decisions.
Example: First_Name, Num1 etc.
Variables, Constants and Arrays ✓ A variable name must start with a letter and NOT
a digit.
What is a variable? ✓ Variables names are case sensitive (Age, AGE,
A variable is a named data store that contains a age are all different names) – when using a
value that may change during execution of the variable, then, check that the case is correct.
program. ✓ Variable names CANNOT contain a space or
special characters like !, #, % etc.
What is a constant? ✓ If two words are used, do not leave a space
A constant is a named data store that contains a between them; you can separate them using an
value that does not change during execution of the underscore or better still use camel case
program. (FirstName) or snake case (firstname): Example
valid/invalid variable names:
What is the difference between a variable and a Variable Name Valid? Reason
constant? firstName YES
FirstName YES
A variable value may change during the program
first_Name YES
execution while a constant does not change First_Name YES
during the program execution (it remains constant). 1STName NO Starts with a digit
First.Name NO Contains a dot(.)
What is an Array: FirstName# NO Contains a #
An array is a data structure containing several Rules for using Constants:
elements (values or variables) of the same data ✓ Constants should be named using meaningful
type; these elements can be accessed using the names that describe the purpose of the variable.
same identifier name. The position of each element For example, a constant used to Interest Rate
in an array is identified using the array’s index. could be named "InterestRate" or simple “Rate”.
What is the difference between a variable, constant CONSTANT Rate = 0.2 (pseudocode)
constRate = 0.2 (python)
and array?
✓ In pseudocode constants are normally declared
Variables and constants store a single value while an
at the start of the program, unless you want to
array stores more than one value (called elements).
restrict their scope.
These elements must be of the same data type with
✓ Constants names should follow the same
position of each element identified using the array’s
rules as those of variables above.
index.
✓ However, constants are declared by starting with
Distinction: the word “CONSTANT” and assigned a value.
Variable Constants Arrays Example:
Named data store Named data store Data structure
that… that… that…
CONSTANT HourlyRate = 20
stores a single stores a single contains multiple CONSTANT Gender = "M"
value that may value that does values called
change during not change during elements that ✓ Only literals (values that do not change) can be used
program program must be of the as the value of a constant. A variable, another
execution execution same data type. constant or an expression must never be used.
Declaring Constants & Variables:
✓ It is considered good practice to declare the ▪ The 1st two lines declare the two variables for age
constants and variables to be used in a program. to store each of the variable (the currentAge and
✓ Some languages require explicit declarations, the total – newAge after addition) .
which specifically state what type of data the ▪ The 3rd line displays a message on the screen
variable or constant will hold. asking the user to enter their current age. Its
✓ Other languages require implicit declarations, good practice to display this message to guide
where the data type is based on the value the user.
assigned. ▪ 4th Line, the user INPUT their age.
✓ Declarations can be at the start of a program or ▪ 5th Line the 50 is added to the INPUT.
just before the data is used for the first time. ▪ 6th Line a text – in 50 years you will be - followed
by the actual value. Note how the text is in
Examples variables and constants declaration: inverted commas and separated from the actual
Example using Pseudocode: variable containing the age by a comma hence
DECLARE FirstNum : INTEGER displaying:
DECLARE SecondNum : INTEGER In 50 years, you will be 70 (for example, if a
CONSTANT FirstConst <- 500 value of 20 was entered as currentAge).
CONSTANT SecondConst <- 100
Worked Example 2:
Note: in pseudocode declarations, variables are
An algorithm is needed to calculate amount a
explicit (state clearly the variable data type) but in
customer would pay for a product after tax of 14% of
python, constants are implicit (does not state the
price. Write a pseudocode for this algorithm.
data type).
Pseudocode:
DECLARE currentAge : INTEGER
DECLARE newAge : INTEGER
OUTPUT “Please enter your age”
INPUT currentAge
newAge <- currentAge + 50
OUTPUT “In 50 years you will be”, newAge
Basic Data Types:
In order for a computer system to process and store Worked Example 3:
data effectively, different kinds of data are formally In pseudocode and the high-level programming
given different types. This enables: language, declare the variables and constants you
✓ data to be stored in an appropriate way, for would use in an algorithm to find the volume of a
example, as numbers or as characters cylinder. Find the volume of the cylinder
✓ data to be manipulated effectively, for example, Note: (Volume = 𝜋𝑟 2 × ℎ).
numbers with mathematical operators and
characters with concatenation Pseudocode:
✓ automatic validation in some cases.
DECLARE radius : REAL
The basic data types you will need to use for IGCSE DECLARE height : REAL
DECLARATION
DECLARE vol : REAL
Computer Science are:
CONSTANT PI <- 3.14
1. INTEGER: a positive or negative whole number OUTPUT “Enter Radius: ”
that can be used with mathematical operators: INPUT Radius
Written as normal in the denary system, e.g. 5, - OUTPUT “Enter Height: ”
3). INPUT Height
vol = PI * radius * radius * height
OUTPUT “The volume is”, vol
2. REAL: a positive or negative number with a
fractional part. Real numbers can be used with Python:
mathematical operators:
Always written with at least one digit on either radius = int(input("Enter Radius: "))
side of the decimal point, zeros being added, if height = int(input("Enter Height: "))
constPI = 3.14
necessary, e.g. 4.7, 0.3, -4.0, 0.0 vol = constPI * radius * radius * height
print(vol)
3. CHAR: a variable or constant that is a single
character: Note: Python does not require any separate
A single character delimited by single quotes, declarations and does not differentiate between
e.g. ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ constants and variables.
Python:
radius = int(input(“Please enter the radius
of the circle”))
Complete Program:
Pseudocode:
DECLARE Radius: INTEGER
DECLARE Area: INTEGER
CONSTANT PI <- 3.14
OUTPUT “Please enter the radius of the
circle”
Area <- PI * Radius * Radius
OUTPUT “Area of the circle is:”, Area
Python:
constPI = 3.14
radius = int(input(“Please enter the radius
of the circle”))
Area = constPI * Radius * Radius
print(“Area of the circle is:”, Area)