0% found this document useful (0 votes)
17 views

Programming Variables Constants Arrays Intro

Uploaded by

Mazvita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Programming Variables Constants Arrays Intro

Uploaded by

Mazvita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming

Chapter Outline:

Programming Concepts:

▪ use of variables and constants


▪ input and output
▪ sequence – selection including nesting
▪ iteration
▪ totalling and counting
▪ string handling
▪ operators
▪ arithmetic, logical and Boolean
▪ procedures and functions including the use of:
o parameters
o local and global variables
o library routines
o creating a maintainable program

Arrays including:
▪ one- and two-dimensional arrays,
▪ use of indexes,
▪ use of iteration for reading from and writing to arrays

File Handling including:


▪ opening, closing, reading from and writing to data and text files.

Learning Objectives:

At the end of this lesson, you will be required to be able to:

✓ 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).

Using Python: Start by identifying the steps required:


FirstNum = 20 Step 1: Declare the price, amount and constant rate,
SecondNum = 60 and the tax.
FIRSTCONST = 100 Step 2: Ask the cashier to enter the price.
SECONDCONST = 300 Step 3: Take the price as INPUT.
Step 4: Assign the variable tax to the product of price
Note: Python does not require any separate and rate (tax=price * rate)
declarations and does not differentiate between Step 5: Assign the variable amount to (tax + price).
constants and variables. Step 6: OUTPUT the amount

Worked Example 1: Pseudocode:


A program needs to ask a person’s age, then tell DECLARE Price : REAL
them how old they will be in 50 years. Write a DECLARE Amount: REAL
pseudocode for the algorithm. DECLARE Tax: REAL
CONSTANT Rate <- 0.14

Start by identifying the steps required. OUTPUT “Enter Price”


✓ Step 1: Declare current age (currentAge) and INPUT Price
age after 50 years (newAge). Tax <- Price * Rate
✓ Step 2: Ask them to enter their age. Amount <- Price + Tax
✓ Step 3: Take the age as input. OUTPUT “The amount payable is”, Amount
✓ Step 4: Add 50 to their age.
Note: how the constant has been declared!
✓ Step 5: Output the new age.

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.

INPUT & OUTPUT


4. STRING: a variable or constant that is several
characters in length. Strings vary in length and ✓ In order to be able to enter data and output
may even have no characters (known as an results, programs need to use input and output
empty string); the characters can be letters statements.
and/or digits and/or any other printable symbol ✓ Algorithms and programs take input from the
(If a number is stored as a string, then it cannot keyboard and output to a screen.
be used in calculations.) ✓ INPUT: For a program to be useful, the user
Delimited by double quotes, a string may contain needs to know what they are expected to input,
no characters (i.e. the empty string) e.g. "This is so each input needs to be accompanied by a
a string", “What is your age?”, " " prompt stating the input required.
Examples:
5. BOOLEAN: a variable or constant that can have Pseudocode/python messages prompting the
only two values TRUE or FALSE. user to enter a value:
OUTPUT “Please enter the radius of the
circle”

Python:
radius = int(input(“Please enter the radius
of the circle”))

This statements in pseudocode and python


display the message on the screen telling the
user what they are required to enter(input).
Note: Python combines the prompt with the
input statement. The “int” preceding the prompt
statement is the data type since all inputs
default to string.
✓ OUTPUT: For a program to be useful, the user
needs to know what results are being output, so
each output needs to be accompanied by a
message explaining the result. If there are
several parts to an output statement, then each
part is separated by a separator character.
Example:
Pseudocode:
OUTPUT “Area of the circle is:”, Area
Python:
print(“Area of the circle is:”, Area)

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)

You might also like