Chapter8 - Programming
Programming concepts
Variables and constants
Python:
# variable
firstVar = 20
# constant
FIRST_CONSTANT = 20
Pseudocode
DECLARE FirstVar: Integer
FirstVar = 10
CONSTANT FirstConst = 500
Basic Data Types
pseudocode
- Integer – a positive or negative whole number that can be used with mathematical operators.
- Real – a positive or negative number with a fractional part; real numbers can be used with mathematical operators.
- Char – a variable or constant that is a single character.
- String – a variable or constant that is several characters in length.
- Boolean – a variable or constant that can have only two values, TRUE or FALSE.
pseudocode
DECLARE FirstInt : INTEGER
DECLARE FirstReal : REAL
DECLARE Female : CHAR
DECLARE FirstName : STRING
DECLARE Flage : BOOLEAN
python::
Python
firstInt = 25
firstReal = 25.0
female = 'F'
firstName = 'Emma'
flag = True
Input and Output
pseudocode
INPUT FirstInt
OUTPUT "Enter a whole number "
OUTPUT "Number is " FirstInt
python
a = input()
b = input("Enter a whole number")
c = int(b)
firstInt = int(input ("Enter a whole number"))
print("number is: ", firstInt)
Basic concepts
Sequence
The ordering of the steps in an algorithm.
An incorrect order can lead to incorrect results or unnecessary extra steps.
Selection
This allows different routes through the steps of a program.
pseudocode
IF Age > 17
THEN
OUTPUT "You are an adult"
ELSE
OUTPUT "You are a child"
ENDIF
CASE OF OpValue
"+" : Answer = Number1 + Number2
"-" : Answer = Number1 - Number2
"*" : Answer = Number1 * Number2
"/" : Answer = Number1 / Number2
OTHERWISE OUTPUT "invalid operator"
ENDCASE
python
if Age > 17:
print("You are an adult")
else:
print("You are a child")
# Python doesn't have switch case
if opValue == "+":
answer = number1 + number2
elif opValue == "-":
answer = number1 - number2
elif opValue == "*":
answer = number1 * number2
elif opValue == "/":
answer = number1 / number2
else:
print("invalid operator")
iteration
- count-controlled loops (for a set number of iterations)
- pre-condition loops – may have no iterations
- post-condition loops – always has at least one iteration.
pseudocode
FOR Counter ← 1 TO 10 STEP 2
OUTPUT Counter
NEXT Counter
WHILE TotalWeight < 100
TotalWeight = TotalWeight + Weight
ENDWHILE
REPEAT
NumberOfItems = NumberOfItems + 1
UNTIL NumberOfItems > 19
python
for Counter in range (1,10,2):
print(Counter)
while TotalWeight < 100:
TotalWeight = TotalWeight + Weight
Totalling and counting
pesudocode
# Totalling
TotalWeight = TotalWeight + Weight
# Counting
NumberOfItems = NumberOfItems + 1
String handling
Strings store text:: every string contains several characters and each character in a string can be found by its position number.
The first character of a string in pseudocode is at position 1; the first character of a string is at position zero for Java, Python and Visual Basic.
- Length – finding the number of characters in the string. For example, the length of the string "Computer Science" is 16 characters as spaces are counted as a character.
- Substring – extracting part of a string. For example, the substring "Science" could be extracted from "Computer Science". First parameter is the string, second parameter is the position of the start character, third parameter is the length of the required substring.
- Upper – converting all the letters in a string to uppercase. For example, the string "Computer Science" would become "COMPUTER SCIENCE".
- Lower – converting all the letters in a string to lowercase. For example, the string "Computer Science" would become "computer science".
pseudocode
LENGTH("Computer Science")
LENGTH(MyString)
SUBSTRING("Computer Science", 10, 7)
SUBSTRING(MyString, 10, 7)
UCASE("Computer Science")
UCASE(MyString)
LCASE("Computer Science")
LCASE(MyString)
Arithmetic, logical and Boolean operators
- arithmetic operators – for calculations (+, -, *, /, ^, MOD, DIV)
- logical – for comparisons
- Boolean – to decide whether an expression is true or false.
Use of nested statements
Selection and iteration statements can be nested one inside another.
Procedures and functions
- A procedure is a set of programming statements grouped together under a single name that can be called to perform a task at any point in a program.
- A function is a set of programming statements grouped together under a single name that can be called to perform a task at any point in a program. A function returns a value back to the main program.
- Parameters are the variables that store the values of the arguments passed to a procedure or function. Some procedures and functions will have parameters.
pseudocode
PROCEDURE Stars
OUTPUT "********"
ENDPROCEDURE
PROCEDURE Stars (Number:INTEGER)
DECLARE Counter : INTEGER
FOR Counter ← 1 TO Number
OUTPUT "*"
NEXT
ENDPROCEDURE
FUNCTION Celsius(Temperature: REAL) RETURNS REAL
RETURN (Temperature – 32) / 1.8
ENDFUNCTION
Python
def Stars():
print("************")
def Stars(Number):
for counter in range (Number):
print("*", end = '')
def Celsius(Temperature):
return (Temperature - 32) / 1.8
Local and global variables
A global variable can be used in any part of a program – its scope covers the whole program.
A local variable can only be used by the part of the program it has been declared in; it is restricted to that part of the program.
Library routines
Library routines are fully tested and ready for use in a program.
- MOD – returns remainder of a division
- DIV – returns the quotient (that is, the whole number part) of a division
- ROUND – returns a value rounded to a given number of decimal places
- RANDOM – returns a random number.
pseudocode
Value1 ← MOD(10, 3)
Value2 ← DIV(10, 3)
Value3 ← ROUND(6.97354, 2)
Value4 ← RANDOM()
python
value1 = 10%3
value2 = 10//3
value = divmod(10,3)
value3 = round(6.97354, 2)
from random import random
value4 = random()
Creating a maintainable program
- always use meaningful identifier names for variables, constants, arrays, procedures and functions
- be divided into modules for each task using procedures and functions
- be fully commented using your programming language’s commenting feature.
pseudocode
// pseudocode uses a double slash to start a comment
python
#Python uses hash to start a comment for every line
8.02 Arrays
One- and tow- dimensional arrays
Declaring and populating arrays with iteration
pesudocode
DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
OUTPUT “Enter these values in order”
OUTPUT “27, 19, 36, 42, 16, 89, 21, 16, 55, 34”
OUTPUT “31, 67, 98, 22, 35, 46, 71, 23, 11, 76”
OUTPUT “17, 48, 29, 95, 61, 47, 28, 13, 77, 21”
FOR ColumnIndex ← 0 TO 2
FOR RowIndex ← 0 TO 9
OUTPUT “Enter next value “
INPUT MyTable[RowIndex, ColumnIndex]
NEXT RowIndex
NEXT ColumnIndex
8.03 File handling
Purpose of storing data in a file
Computer programs store data that will be required again in a file.
Using files
pseudocode
DECLARE TextLine : STRING
DECLARE MyFile : STRING
MyFile ← "MyText.txt"
// variables are declared as normal
// writing the line of text to the file
OPEN MyFile FOR WRITE // opens file for writing
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE, TextLine // writes a line of text to the file
CLOSEFILE(MyFile) // closes the file
// reading the line of text from the file
OUTPUT "The file contains this line of text:"
OPEN MyFile FOR READ // opens file for reading
READFILE, TextLine // reads a line of text from the file
OUTPUT TextLine
CLOSEFILE(MyFile) // closes the file