2.2 Programming Fundamentals Flashcards
(17 cards)
What is sequence?
The order in which instructions are executed in a program.
In a computer language it means that programming statements are set out one after the other.
What is selection?
Determining which choices you have to make.
You can use conditional statements like IF and CASE
What is iteration?
The repetition of a task is called iteration.
It can include things like:
FOR loops
WHILE loops
REPEAT or DO loops
What does MOD do?
Calculates the remainder of a division operator.
e.g. 10 MOD 3 = 1
What does DIV do?
It divides two numbers and returns the whole number part of the result, discarding any remainder.
e.g. 9 DIV 2 = 4
What does .length do?
Finds the length of a string e.g. wordlength = subject.length
What does .subString do?
Returns the part of a string that you take out. e.g.
Letter = subject.subString (0,1)
0 is the starting index and 1 is the number of characters you want
What does .left do?
Returns the number of letters you want from the left of a string e.g.
Text = subject.left(4)
4 is the number of elements in the string that are outputted
What does .right do?
Returns the number of elements you want from the left of a string e.g.
Text = subject.right(3)
3 is the number of elements in the string that are printed out
What does .upper and .lower do?
Convert a string between lowercase and uppercase.
How to use open in file handling?
MyFile = open(“sample.txt”)
How to read to file?
// You must use the same file object name consistently e.g. myFile
// This will read in the next line in the file object
// and assign it to the inline String variable
inline = myFile.readLine()
How do you write to file?
myFile.writeLine(“Add new line\n”)
myFile = open(“sample.txt”)
myFile.writeLine(“Add new line”)
myFile.close()
What is the code for reading and writing to the file?
// Open the file and declare variables
myFile = open(“Names.txt”)
array String szNames[]
int iCount = 0
int iNumNames = 0
// Count how many names currently exist in the file
while NOT myFile.endOfFile()
myFile.readLine()
iNumNames = iNumNames + 1
endwhile
// Close and reopen the file to reset the cursor back to the start of the file
myFile.close()
myFile = open(“Names.txt”)
// Set the size of the array plus 1 for the new name
iNumNames = iNumNames + 1
szNames[iNumNames]
// Read in the file into the array
while NOT myFile.endOfFile()
szNames[iCount] = myFile.readLine()
iCount = iCount + 1
endwhile
// User enters a name which is added to the end of the array
szNames[iCount] = input(“Enter a name: ”)
// Write each line in the array to file
for i = 0 to iNumNames-1
myFile.writeLine(szNames[i] + “\n”)
next i
// Close the file
myFile.close()
How to use random numbers?
// Create a random integer between 1 and 6 inclusive.
int randNum = random(1,6)
What is a database?
A database is a collection of data or information which is held together in an organised or logical way.