programming Flashcards
what is a variable
a memory location that is used to temporarily hold data value,
it can change while program is running
benefits of subprograms:
easier to maintain: code is shorter, easier to understand
avoids repetition of code: sub can be called instead of copying/pasting, quicker to develop new programs
work can be delegated between team members, reduces development time
count controlled loop to print out numbers 1 to 10 in ascending order
for i = 1 to 10
print i
next i
what is high level language:
aimed to be understood by humans, has to be translated before it can be run
Integrated Development Environment tools:
editor, to enable program code to be entered/ edited
translator, to enable code to be executed
three programming constructs:
selection
iteration
sequence
local variable advantages
saves memory - only uses memory when that local variable is needed - global variables use memory even if not used
easier to debug local variables as they can only be changed within one subroutine
you can reuse subroutines with local variables in other programs
global variable advantages
variables can be used anywhere in the whole program (and in multiple subroutines)
makes maintenance easier as they are only declared once
can be used for constants - values that remain the same
what is casting?
changing the data type of a variable to another data type
why is casting needed?
-to concatenate two strings, the ALU needs to use numbers to perform the arithmetic operation
-therefore string needs to be ‘cast’ to a number
-integers require less bits of memory than real. numbers
what is a boolean data type?
-true or false
-used to track if something has happened or happened or not
return a string length:
length = string.length
convert a string to all upper/lower case
ustring = string.upper
lstring = string.lower
extract the ‘mat’ letter from
string = “tomato”
chars = string.substring(2,3)
output PUT from someText = “Computer Science”
print ((someText.substring(3,3).upper)
generate a random number betw 1 and 6:
myNum = random(1,6)
advantages of using subprograms
- program is easier to write
- program is easier to debug
- creating reusable components
- functions could be gathered together into a library for easy reuse across multiple programs
difference between functions and procedures
- functions return values and create reusable program components
- procedures create modular structure to a program making it easier to read
create a function called ‘greeting’ to concatenate a name with hello
procedure greeting(“name”)
print(“hello” + name)
endprocedure
describe how a character set is used to represent the string value stored in a variable
-each character has unique binary value
-binary value of each character store/combined (in order)
Use pseudocode to write an algorithm that does the following :
*Inputs the title and year of a book from the user.
*Uses the librarycode function above to work out the book code.
*Permanently stores the new book code to the text file bookcodes.txt
title = input(“enter title”)
year = input (“enter year”)
code = librarycode(title, year)
myFile.writeLine(librarycode(title, year))
MyFile.close()