Unit 1 - Fundamentals of programming Flashcards
What is an algorithm?
A sequence of instructions that can be followed to solve a problem.
What can relational operators be used for?
To compare values within the expression
What are the three types of iteration?
Indefinite iteration with the condition tested at the start of the loop. (While)
Indefinite iteration with the condition tested at the end of the loop.
Definite iteration where the loop is performed a given number of times. (For loop)
What is a subroutine?
A set of instructions with a name
What is a function?
A subroutine that returns one or more values.
What are library subroutines?
Pre-defined subroutines.
Where do parameters appear?
In subroutine definitions. They are part of the subroutine definition.
Where do arguments appear?
In subroutine calls. They may vary from call to call.
What is a local variable?
It can only be used in that subroutine, they are created once a method is entered and cleared once the method is exited.
What is a global variable?
it is defined in the main program and can be used in any subroutine called from the main program.
What is the scope of variables?
The scope of a local variable is the subroutine which it is declared. The variable does not exist outside the subroutine.
What are advantages of local variables?
The subroutines will be independent of a particular program and can be re-used in different programs.
There is no chance of accidentally changing a variable in the main program that is used in a subroutine or vice versa.
Keep your subroutines self-contained!
Pass as arguments any values that are needed.
What is modular programming?
Means breaking down a major task into smaller subtasks.
What are some advantages of modular programming?
Large programs are broken down into subtasks that are easier to program and manage.
Each module can be individually tested
Modules can be re-used several times in a program.
Large programs are much easier to debug and maintain.
Programs are more easily and quickly written.
What does a file consist of?
A number of records.
What is a record?
A data structure consisting of a number of fields which can all be of different data types
What is a text file?
A text file has fields typically separated by commas, and written one line at a time.
You can create, read and manipulate a text file with any text editor.
How do you write a file?
file=open(‘newFile.txt’,’w’)
file.write()
What is a binary file?
A binary file can contain records with different types of field such as string, integer, real, Boolean.
Binary files contain bytes.
You can write data to a binary file
How do you read a file?
file=open(“newFile.txt”, “r”)
file.read()
How do you append to a file?
file=open(‘newFile.txt’,’a’)
Why use a binary file?
Text files are convenient because you can read and manipulate them with a text editor, but they can only store a string of characters
Sometimes you need to store a more complex record structure and read it back with the fields intact
A record can store any mix of data types in one unit of data which can be written to a file and read back
What is a runtime error?
A runtime error will cause a program to crash.
Why do you use exception handling?
Using exception handling statements, you can intercept and handle exceptions so that your program does not crash.
How do you code exception handling?
Insert a try… except statement to ensure that it does not crash
What are some examples of file handling errors?
Trying to open a file which is not in the specified or default folder.
Reading past the end of the file.
Why use a constant?
Constants reduce the risk of errors by reducing access to the memory location
e.g. pi is 3.14159…
How can you change the value of a constant?
You have to change it in the source code and then recompile.
How can you change the value of a variable?
The value of a variable can be changed while the program is running.
What can boolean operators be used for (and, or)?
To link multiple expressions.
What is a case statement?
The logical equivalent to the if…then or elif statement.
It can be used to evaluate multiple expressions
What does iteration mean?
Iteration means repetition
A sequence of instructions is repeated multiple times
This is much more efficient than writing the instructions multiple times
What is an infinite loop?
You may cause an infinite loop if you make a coding error.
Computer control and data sensing applications use infinite loops to gather data from sensors. After the setup code is run, the device enters an infinite loop to repeatedly check the value of the sensors
What is an array?
a data structure containing multiple elements all of the same data type, e.g. string, integer, real, etc.