2.2.1 Programming techniques Flashcards

PMT Adv. Notes (W/ cal 26/46, Checked 27/46) (46 cards)

1
Q

What’s a…

Programming Construct purpose?

A

Simplifies problem = easy to understand & program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s…

4 Programming Constructs

Structured programming

A
  1. Sequence
  2. Selection
  3. Iteration
  4. Recursion

Recursion is a type of iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s…

Sequential programming?

Programming construct (1/4)

A

Code executed:

  • line-by-line
  • top to bottom
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s…

Selection?

Programming construct (2/4)

A

Code runs if specific condition met.

  • Using IF statements.

Also known as ‘Branching’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s…

Iterative programming

Programming construct (3/4)

A

Code executed X no. of times or while condition met.

Uses FOR, WHILE or REPEAT UNTIL loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How’s…

Iteration controlled?

A
  • Count-controlled
  • Condition-controlled
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s a…

Count-controlled loop?

Iterative programming

How programmed?

A

Repeated X no. of times.

for i in range (0,10):
— print i
next i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s…

Condition-controlled loop?

How programmed?

A

Continues until a condition met.

while i <= 20:
— print “Not true”;
— i=i+1
endwhile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s…

Recursion?

Programming construct (4/4)

What’s stopping condition?

A

Subroutine calls itself during its execution.

Condition met = recursion stops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use…

Recursion over Iteration?

A
  • Suits certain problems.
  • More easily expressed.

Both produce same result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s…

Recursion’s main advantage?

Why need to clearly defined?

A

Uses fewer lines of code = less errors.

NB: Not all problems

Reach stopping condition after a finite no. of function calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How’s…

Recursion programmed?

A

function factorial(number)
if number == 0 or 1:
—— return 1
— else:
—— return number * factorial(number - 1);
— endif
end function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s a…

Stack frame?

Recursive programming

When?

A

Stores data within call stack - belonging to subroutine:

  • Parameters
  • Local variables
  • Return addresses

Each time fuction calls itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s a…

Call Stack?

Recursive programming

A

Data structure storing:

  • Parameters
  • Local variables
  • Return addresses

From currently running subroutines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When does…

Subroutine unwind?

Recursive programming

A

When base case/stopping condition reached

Finite no. of stack frames are created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s…

Recusion disadvantages?

Recursive programming

A
  • Inefficient use of memory
  • Risk of stack overflow
  • Difficult to trace
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What’s…

Stack overflow?

Recursive programming

A

Subroutine calls itself too many times = call stack runs out of memory = program crashes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What’s…

Tail recursion?

A

More efficient = use less stack space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What’s…

Scope?

What’s 2 types?

A

Section of code where variable = available.

  1. Global
  2. Local

20
Q

What’s a…

Local variable?

A
  • Limited scope
  • Only accessible in block of code where defined.

diff subrouines with same local variable name != effect others

21
Q

What’s…

Local variables benefits?

A
  • Good programming practice.
  • Subroutines self-contained.
  • Not affected by code outside subroutine.
  • Less memory as deleted after subroutine use.
22
Q

What’s…

Global variables?

A
  • Wide scope
  • Accessable across whole program.

Variables in main body = automatically global.

23
Q

What’s…

Global variables negatives?

Positives?

A
  • Unintentionally overwritten & edited
  • Require more memory as not deleted until program terminates

  • useful for values used by multiple parts of program

24
Q

What’s…

Modular programming?

A

Technique splits large, complex programs –> smaller, self-contained modules.

25
# What's... **Modular** programming's **advantages**?
* Easier to **understand** & approach problems. * Easier to **divide** tasks & manage * **Simplifies** testing & maintenance (components dealt individually). * Improves **reusability** of components (tested module can be reused).
26
# What's... **Top-down approach**? | Modular programming
* Technique to **modularise** programs. * problem **continually split** --> sub problems. (until all = individual **blackbox** performs certain task) | Also known as **stepwise refinement**.
27
# What's a... Procedure? | Subroutines
* Named block of code performs specific task. * Don't need to return value or can return multiple values * Procedures are typically given data as parameters for manipulation
28
# What's a... Function? | Subroutines
* Named blocks of code performing specific task. * Must return single value * Commonly use local variables .
29
# What's... Parameter? | Subroutines
Value passed into a subroutine.
30
# How are... Parameters passed into a subroutine?
* By value * By reference
31
# What happends when... Parameter passed by value?
effectively treated as a local variable ; a copy of the value is passed to the subroutine and discarded at the end therefore its value outside of the subroutine will not be affected.
32
# What happends when... Parameter passed by reference?
Passing by reference means that the address of the parameter is given to the subroutine, so the value of the parameter will be updated at the given address.
33
# What's... IDE? | Integrated Development Environment ## Footnote *Examples?*
a program which provides a s et of tools to make it easier for programmers to write, develop and debug code . ## Footnote *PyCharm, Eclipse, IDLE and Microsoft Visual Studio.*
34
# What's... 5 common IDE features?
1. Stepping 2. Variable watch 3. Breakpoint 4. Source code editor 5. Debugging tools
35
# What's... Stepping? | IDE features (1/5)
This allows you to monitor the effect of each individual line of code by executing a single line at a time.
36
# What's... Variable watch? | IDE features (2/5)
Sometimes used to pinpoint errors, this is a useful feature to observe how the contents of a variable change in real-time through the execution of a program.
37
# What's... Breakpoint? | IDE features (3/5)
IDEs allow users to set a point in the program at which the program will stop. This can either be based on a condition or set to occur at a specific line. This can help to pinpoint where an error is occurring.
38
# What's... Source code editor? | IDE features (4/5)
The editor aims to make the coding process easier by providing features such as autocompletion of words, indentation , syntax highlighting and automatic bracket completion.
39
# What's... Debugging tools? | IDE features (5/5)
Some IDEs also provide run-time detection of error s with a guide as to where in the code they are likely to have occurred through line numbers and highlighting.
40
# What's... A class? | Object-oriented programming
a template for an object and defines the state and behaviour of an object .
41
# What's... An attribute? | Object-oriented programming
give an object’s properties .
42
# What's... A Method? | Object-oriented programming
Actions the class can perform.
43
# What's.. An Object?
a particular instance of a class
44
# What's... Instantiation?
a process where classes can be used to create objects
45
# What's... Encapsulation?
attributes are declared as private so can only be altered by public methods. used throughout programming to implement the principle of information hiding.
46
# What's.. Information hiding?
programs are made less complex by protecting data from being accidentally edited by other parts of the program. | Top-down design use encapsulation, as each module is self-contained.