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)