Programming concepts Flashcards
What does a variable do?
Holds values that can be modified when the program is executed
What does a constant do?
Holds values that remain unchanged when a program is executed
How do you assign a variable?
Declare the name and data type, then assign a value
What are the three main steps to using variables?
Declaration, Assignment, and Initialisation
What is declaration?
Creating space in memory for a variable or constant
What is assignment?
Setting the contents of the memory space to a value
What is initialisation?
Setting the value of a variable before any processing takes place
Name the advantages of using constants
You can use the same identifier throughout a program which keeps the program code consistent- making it easy to debug and read, if errors occur changes only need to be made at one point- the initialising of the constant
Define an identifier
A name given to any variable, constant, subroutine, or class
Why are meaningful identifiers important?
So the purpose of the subroutine is clear to anyone reading your code
What are definite loops and how are they implemented? give an example
A set of instructions repeated a certain number of times, implemented by count-controlled loops e.g. for
What are indefinite loops and how are they implemented? give an example
A set of instructions that are repeated until a condition is met, implemented by condition control loops e.g. while
When does a while loop check its condition?
At the start of each iteration until the condition is met
When does a repeat until loop check its condition?
At the end of each iteration until the condition is met
What is nested iteration?
When a loop is inside another loop
How does nested iteration work?
The inner loop will be executed for all the iterations of the outer loop
Define sequence
A series of instructions that a program executes in order, one after another
How does selection differ from sequence?
Selection executes a program based on a condition
What is nested selection used for?
Implementing branching logic
What is branching logic?
If a condition of the main selection block evaluates to true then it leads to sub-conditions which are included inside the main condition
What are operands?
The values in calculations to which arithmetic operators are applied
`What does DIV do?
Returns ONLY the whole number part of a division, the fractional part is dismissed
What does MOD do?
Returns ONLY the remainder of a division
What does Round() do?
Rounds up the result of an operation
What does Truncate do?
Rounds down the result of an operation
What are relation operators used for?
Comparing the values of 2 variables or comparing a given value against a variable
What do expressions with relational operators evaluate to?
True or False
What are logical operators used for?
Combining multiple factors in a program using logical operators
What do expressions with logical operators evaluate to?
True or False
When does an AND statement evaluate to True?
When all operands equal True
When does an OR statement evaluate to True?
When either of the operands is True
When does a NOT statement evaluate to True?
When the operand is equal to False as it reverses its value
What is the default order of operations in logical statements(when no brackets are present)?
- NOT 2. AND 3. OR
How are arrays and strings similar?
Each character in a string is indexed but it is immutable( once it’s created it can’t be changed)
Define exception
An exceptional or unexpected event that occurs when a program is running
What is exception handling?
The way a program deals with exceptional circumstances without crashing
What is a subroutine?
A section of code that can be called by writing the subroutines name in a statement
What are the 2 types of subroutine?
Function and procedure
What’s the difference between a procedure and a function?
Procedures simply execute a set of instructions whereas functions return a value after running
Advantages of subroutines
Makes code readable and reusable, shorten programs, subroutines can be tested separately,
What does a parameter do?
Defines the data that must be passed into a subroutine when it’s called
What is an argument?
A piece of data that is passed into a subroutine
Which type of subroutine returns a value?
Function
What is the scope of a variable?
Where in the program a variable or constant can be accessed
Define local variable
A variable that is only accessible within a specific part of a program
Do local variables in different subroutines need different names and why?
No, because they’re kept in separate
Define global variable
A variable that can be accessed anywhere in the program
Where are global variables defined?
Outside of the subroutine
What’s the problem with global variables?
Their values can be altered anywhere so can be changed accidentally
if a local variable and global variable have the same, which takes precedence?
The local variable will be taken first but will not override the global variable
What is a call stack?
An area of memory allocated to a program to store information about each active subroutine
What is an active subroutine?
A subroutine that’s been called but hasn’t finished executing
What does a stack frame store?
The value of any local variables ad the value of any parameters
What is a recursive algorithm?
An algorithm that calls itself
What’s a general case?
A statement that expresses the solution to a problem in terms of a reference to a smaller version of that same problem
What’s the base case?
The point at which you know the answer to a specific version of a problem
What are the criteria for a recursive algorithm, in order?
Define a base case, define the general case
Write the general case for any function
sum_to_n(n) = n + sum_to_n(n-1)
Write the base case for any function
sum_to_n(1) = 1
Are parameter values passed byval or byref in recursive routines?
Byval
Advantages of recursion
Natural way to process data structures that are recursive by nature( like trees), Fewer lines of code
Disadvantages of recursion
Harder to trace, uses more memory, slower to run