Programming Techniques Flashcards
What are the three programming constructs?
Sequence,
Selection (Branching),
Iteration
What is Sequence?
Executing Instructions one after another in order (e.g. Line 1, Line 2, Line 3, etc.)
What is Selection?
Conditional Statements that can change the flow and order of execution in code (e.g. if and elif in python)
What is Iteration?
Repeating the same block of code multiple times (for and while in python)
What is a Boolean Expression?
An evaluative statement that results in a boolean output (e.g. num1 == num2)
What is the difference between a pre-test loop and post-test loop?
Pre-test loop: the conditional test is done before the body of the loop is executed
Post-test loop: the conditional test is done after the body of the loop is executed
What is the usefulness of a switch/match case statement?
Keeps code cleaner by simplifying the many if statements with simple to read cases
e.g.
switch(value) {
case 1:
//Code
break;
}
What is a global variable?
A variable that is created at the start of a program and exists until the end, this variable is stored in the Heap
What is a local variable?
A variable that is created at the start of a sub-program and is freed from memory when the sub-program ends, this variable is stored in the Stack
What is the stack?
A section of program memory that allows for values to be pushed onto it and popped off of it, it is a first in, last out data structure (like a stack of plates)
What is the heap?
A defined section of memory that holds variables placed within it at the pointed to location, no order of data in and out
Why would we want to and not want to use global variables?
Global variables could be used for data that is commonly accessed across the program like score, however can be confusing when there are loads of them especially in larger projects that span 1000s of lines and many different files and modules
What is garbage collection?
When the high level language’s runtime environment handles local variables pushing them onto the stack when a subroutine starts and popping them off the stack when the subroutine exits
What actually happens to free a section of the stack?
The stack pointer decrements preventing the program from accessing the “removed” data, which will be overridden when more data is pushed onto the stack