2.2 Programming Fundamentals Flashcards
(48 cards)
Define variable
A value stored in memory that can change while the program is running
Define constant
A value that does not change while the program is running, and is assigned when the program is designed
Define assignment
Giving a variable or constant a value
Define casting
Converting a variable from one data type to another
Define input
A value is read from an input device e.g. keyboard
Define output
Data generated by the computer and displayed to the user
Advantages of constants
1) make a program easier to read as they are declared and assigned at the top of the program
2) can be changed easily by the programmer in one place in a program
3) instead of changing every instance of a value throughout a program
4) so less chance of errors
5) compiler can optimise code
6) which makes a program run more quickly
Why is casting needed?
Inputs from the keyboard are always characters (multiple characters are called strings). However, for calculations, the ALU must use numbers so the string is cast to a number.
What is sequence?
Executing one instruction after another
What is selection?
A program branching depending on a condition
What is iteration?
Repeating sections of code
When are FOR loops used?
Count-controlled loops. Used when the number of iterations needed is known ahead of the iteration executing.
When are WHILE loops used?
Condition-controlled loops. Used when the number of iterations is not known because the variable used to determine when the iteration ends is changing within the iteration itself
When are DO…UNTIL loops used?
An alternative to WHILE where the code executes at least once before the condition is checked
Casting to an integer
int()
Casting to a real/float
real()/float()
Casting to boolean
bool()
Casting to a string
str()
Casting to ASCII
ASC(b)
Casting to a character
CHR(98)
Example of a FOR loop
for rolls in range():
Example of a WHILE loop
while answer != “Computer”:
answer = input(“Pass: “)
endwhile
Example of a DO…UNTIL loop
do
answer = input(“Pass: “)
until answer == “Computer”
Example of NOT
end_of_file = False
while not end_of_file