Section Eleven: Programming techniques Flashcards
Chapter 53 – Programming basics
What is an algorithm?
A sequence of unambiguous instructions for solving a problem.
Chapter 53 – Programming basics
Pseudocode
Pseudocode is a way of expressing an algorithm using syntax that is independent of any particular programming language. The algorithm can then be coded in a suitable programming language
Chapter 53 – Programming basics
Comments
A programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.
Chapter 53 – Programming basics
Data types
- integer a whole number such as -25, 0, 3, 28679
- real/float a number with a fractional part such as -13.5, 0.0, 3.142, 100.0001
- Boolean a Boolean variable can only take the value TRUE or FALSE
- character a letter or number or special character typically represented in ASCII, such as a, A, %, ? or %. Note that the character “4” is represented differently in the computer from the integer 4 or the real number 4.0
- string anything enclosed in quote marks is a string, for example “Peter”, “123”, or “This is a string”. Either single or double quotes are acceptable.
Chapter 53 – Programming basics
Common arithmetic operations
The symbols +, -, * and / are used for the common arithmetic operations of addition, subtraction,
multiplication and division.
Chapter 53 – Programming basics
The Round function
You can round this number using a function round.
billBetween3 = round(billBetween3,2) //round to 2 decimal places
This will return the value 6.67.
Chapter 53 – Programming basics
Exponentiation
If you want to find, for example 25, 5 is called the exponent and you need to use exponentiation.
You can write this operation in pseudocode as
x = 25
or, using variables,
x = yn
Chapter 53 – Programming basics
String-handling functions
Programming languages have a number of built-in string-handling methods or functions. Some of the common ones in a typical language are:
- len(string) Returns the length of a string
- string.find(str) Determines if str occurs in string. Returns index (the position of the first character in the string) if found, and -1 otherwise. In our pseudocode we will assume that string (1) is the first element of the string, though in Python, for example, the first element is string (0)
- ord(“a”) returns the integer value of a character (97 in this example)
- chr(97) returns the character represented by an integer (“a” in this example)
Chapter 53 – Programming basics
Constants and variables
A constant is a data item whose value cannot change during the program’s execution. Thus, as its name implies – the value is constant.
A variable is a data item whose value can change during the program’s execution. Thus, as its name implies – the value can vary.
Chapter 53 – Programming basics
String conversion operations
- int(“1”) converts the character “1” to the integer 1
- str(123) converts the integer 123 into a string “123”
- float(“123.456”) converts the string “123.456” to the real number 123.456
- str(123.456) converts the real number 123.456 to the string “123.456”
- date(year,month,day) returns a number that you can calculate with
Chapter 53 – Programming basics
Standards for variable names
Guidelines could include:
- Start all variable names with a lowercase letter
- Do not use underscores in the middle of variable names
- Use “camelCaps” to separate parts of a variable name – for example, timeInMinutes, maxTemperature
- Do not use overly long names but keep them meaningful – maxTemp is better than maximumTemperature if there is not likely to be any confusion over the meaning of max
- Use all uppercase letters for constants, which are then instantly identifiable
- When defining a class in object-oriented programming, start with an uppercase letter, with the rest of the class name lowercase
Following guidelines such as these will save a lot of time in looking through a program to see whether you called something best_score, Best_Score, bestScore or some other variation.
Chapter 54 – Selection
Selection
Selection statements are used to select which statement will be executed next, depending on some condition. Conditions are formulated using relational operators.
Chapter 54 – Selection
Relational operators
The following operators may be used in pseudocode for making comparisons:
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal
!= not equal
Chapter 54 – Selection
The switch/case statement
Some programming languages support the use of a switch or case statement, an alternative structure to a nested if statement. It is useful when a choice has to be made between several alternatives.
Chapter 54 – Selection
Boolean operators AND, OR, NOT
More complex conditions can be formed using the Boolean operators AND and OR.
Chapter 54 – Selection
The NOT operator
You can usually avoid the use of the NOT operator, replacing it with an appropriate condition. e.g.
NOT (a = b) is equivalent to a != b
NOT (a < b) is equivalent to a >= b
Chapter 54 – Selection
The XOR operator
XOR stands for exclusive OR, so that a XOR b means “either a or b but not both”.
This can be implemented with a combination of AND, OR and NOT conditions:
(a AND NOT b) OR (NOT a AND b)
Chapter 55 – Iteration
Performing a loop
The third basic programming construct is iteration. Iteration means repetition, so iterative statements always involve performing a loop in the program to repeat a number of statements. There are three different types of loop to be
considered, although some programming languages do not implement all three.
Chapter 55 – Iteration
The while / endwhile loop
A while … endwhile loop has two properties:
- The expression controlling the repetition of the loop must be of type Boolean – that is, one which evaluates to True or False
- This expression is tested at the start of the loop
Chapter 55 – Iteration
The repeat until loop
This type of loop is very similar to the while … endwhile loop, with the difference that the Boolean expression controlling the loop is written and tested at the end of the loop, rather than at the beginning. This means that the loop is always performed at least once.
Chapter 55 – Iteration
The for next loop
This type of loop is useful when you know how many iterations need to be performed. For example, suppose you want to display the two times table:
for count = 2 to 12
product = 2 * count
print(“2 x “, count, “ = “, product)
next count
The value of count starts at 2 and is incremented each time round the loop. When it reaches 12, the loop terminates and the next statement is executed.
Chapter 56 – Subroutines and recursion
Types of subroutine
A subroutine is a named block of code which performs a specific task within a program. Most high-level languages support two types of subroutine, functions and procedures, which are called in a slightly different way. Some languages such as Python have only one type of subroutine,
namely functions. All programming languages have ‘built-in’ functions which you will have already used if you have written any programs. For example, in Python:
myName = input(“What is your name? “)
print(“Hello, “, myName)
Chapter 56 – Subroutines and recursion
Passing parameters by value and by reference
Frequently, you need to pass values or variables to a subroutine. The exact form of the subroutine interface varies with the programming language, but will be similar to the examples below:
procedure subroutineName(parameter1, parameter 2,…)
function subroutineName(parameter1, parameter 2,…)
In some programming languages, parameters may be passed in different ways. If a parameter is passed by value, its actual value is passed to the subroutine, where it is treated as a local variable. Changing a parameter inside the subroutine will not affect its value outside the subroutine.
All parameters are passed by value in Python.
In Visual Basic or Pascal, parameters may be passed by value but they may also be passed by reference. In this case, the address, and not the value, of the parameter is passed to the subroutine. Therefore, if the value is multiplied by three, for example, its value in the main program will reflect that change since it is referring to the same memory location.
To pass by reference in Pascal, the procedure header will specify that the relevant parameter is a variable.
For example:
procedure abc(x, y : integer; var z : integer;)
Here, x and y are passed by value and z is passed by reference.
Chapter 56 – Subroutines and recursion
Local and global variables
Variables used in the main program are by default global variables, and these can be used anywhere in the program, including within any subroutines. Within a subroutine, local variables can be used within the subroutine, and these exist only during the execution of the subroutine. They cannot be accessed outside the subroutine and changing them has no effect on any variable outside the subroutine, even if the variable happens to have the same name as the local variable.
In Python, variables used in subroutines are local by default, unless they are declared as global in the calling program.