Programming construct Flashcards

(60 cards)

1
Q

Variables (meaning and indication for memory use)

A

> Variable is a name to refer to a particular memory location that is used to store data.
Variables have data types:( String, Boolean, integer, or decimal ) which will indicate how much memory they will use and the type of data they will store.

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

Procedural language

A
>  .upper()
> >lower()
> .string slicing()
> print()
> .length
> .substring
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Selection statements (meaning)

A

Selection statements carry out a set of commands on a condition. This allows your code to make choices - “Branching”

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

Selection - IF statements

A
IF [condition] then
        [code for true]
endif
------------------------------------------------------------------------------------------
IF [condition] then
         [code for true]
else
         [code for false]
endif
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Selection - IF statements with multiple branch

A
IF  [condition]  then
         [code for true]
elseif [condition 1 ] then
         [code for true 1 ]
else
         [code for false, false 1]
endif
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

selection - switch case

A
switch entry:
       case "A":
              print(................)
       case "B":
              print(................)
       default:
             print(..........................)
endswitch
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Relational operators

A
    " == "                                  " Equal to" 
    " != "                                 "Not equal to"
    " <  "                                  " less than"
    " <= "                             " less or equal to "
    " >  "                                " greater than "
    " >= "                          " greater or equal to "
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

logical operators

A

> AND = Return true if both conditions are true
OR = Return true if any of the conditions are true
NOT = returns the out come of the expression ,true becomes false and false becomes true

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

Select…… Case…… End case (pseudocode)

A
print("1. add number")
print("2. subtract numbers")
print("3.quit")
choice = input("enter your choice")
switch choice
          case "1"
                  print("adding numbers chosen")
          case "2"
                  print("subtracting numbers chosen")
          case "3"
                  print("quit chosen")
            default:
                 print("pick between choice 1 ,2 and 3")
endswitch
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

sequence

A

Sequence is when the algorithm is in logical order in step by step format

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

Iteration/loops

A

An Iteration is a section of code that gets repeated

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

counter controlled iterations(meaning and

A

> counter controlled iterations are used when you want to illiterate a known number of times
Counter controlled iterations use a variable which can be used within the code to know how many times the code has been run.
Code is indented within an iteration to make it easier to read.

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

For… Next (Pseudocode)

A

For counter = 1 to 5
print(counter)
Next counter

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

Condition Controlled loops

A

Condition-controlled iteration repeatedly executes a section of code until a condition is met - or no longer met.

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

While… Loop

A
counter=0
	While counter < 3
		print(counter)
		counter=counter + 1
	endwhile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Do…Until loop

A
counter=0	
	Do
		print(counter)
		counter=counter + 1
	Until counter == 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Variable

A

A name used to refer to a particular memory location that is used to store data.
The value of the data held in that memory location is not known when the program is written and can change while the program is running.

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

Constant

A

Defined at the start where it can be changed if necessary and cannot be changed during execution
Makes reading the code more understandable
Eg: Const InterestRate AS Double = 0.2

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

Local variables

A

> A variable defined within one part of program or module.
It is only accessible in that part of the program or module.
Data contained is lost when execution of that part of program or module is completed
The same variable names can be used in different modules
Eg: loop counter

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

Global variables

A

> A variable that is defined at the start of a program.
It exists throughout program including functions/procedures
Allows data to be shared between modules
Overridden by local variables with the same name
Eg: VAT rate

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

Difference between Local and Global Variables

A

Local variable is visible only in module or construct where it is created or declared and Global variable is visible throughout a program or may be accessed from more than one part of the program

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

Why good programming practice generally avoids the use of global variables

A

> Global variables make it difficult to integrate modules and they increase complexity of a program.
Global variables may cause conflicts with names written by others in other modules

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

Parameters

A

> Information about an item of data supplied or transferred to a function or procedure.
Pass values between functions and procedures.
Can be passed by reference or by value
Used as a local variable if it is BY Val

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

Why parameter passing to a function can be a better alternative to using global variables.

A

> When a parameter is passed to a function, only a copy of the data passed may be changed therefore no unforeseen effects will occur in other modules.
This is the case only if passed by value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Subroutines
> Subroutines are sections of code to break a longer programs into smaller pieces. > Thereby making them easier to read and more manageable for teams of programmers to work together on one program. > Subroutines are also known as procedures.
26
Procedure pseudo code example
number=input(“How many numbers do you want to output?”) max=input(“What is the maximum number?”) outputrandoms(number, max) ``` Procedure outputrandoms(n:byVal, m:byVal) for counter = 1 to n randomnum=int(random(1,m)) print(“Number “,counter,” is “,randomnum) next ```
27
Procedure meaning
A procedure is a self contained set of commands that can be called from different parts of a program.
28
Why do we need procedures in coding?
> It helps to break a problem down to manageable sections. > It helps to prevent duplicating sections of code when it is needed more than once in a program. > It makes the code easier to read, debug and maintain.
29
What is a function?
> A function is a sub-routine that may take one or more parameters and returns a value. It uses local variables .
30
What is a function?
> A function is a sub-routine that may take one or more parameters and returns a value. It uses local variables .
31
Function pseudocode example
num1=int(input(“Enter the first number: ”)) num2=int(input(“Enter the second number: ”)) Print(“Product of two numbers is: “, product(num1,num2)) ``` function product(a:byVal, b:byVal) answer = a * b Return answer endfunction ```
32
How to write data to a file?
myFile = openWrite(“sample.txt”) myFile.writeLine(“Hello World”) myFile.close()
33
How to read data from a file
myFile = openRead(“data.txt”) x =myFile.readLine() myFile.close()
34
How to use loop to read data from a file
``` myFile = openRead(“data.txt”) while NOT myFile.endOfFile() print(myFile.readLine()) endwhile myFile.close() ```
35
What does IDE stand for? What do you need to write a program? What do you need to Translate a program? What do you need to Assemble the compiled code into one big program?
IDE stands for Integrated Development Environment Write a program – you need text editor Translate a program – you need an assembler/ compiler/ interpreter Assemble the compiled code into one big program – you need a linker.
35
What is IDE?
A single program used for developing programs made from a number of components.
36
At the very least, an IDE will probably include:
> An editor for writing the source code > Facilities for automating the build > A debugger > Features to help with code writing, such as pretty printing and code completion
37
Common features found on IDE
> [Debugging tools] allow inspection of variable values this can allow run-time detection of errors. > Code can be examined as it is running which allows logical errors to be pinpointed. > [IDE debugging] can produce a crash dump, which shows the state of variables at the point where an error occurs. > It can display stack contents which show the sequencing through procedures/modules. > It can step through code , which allows the programmer to watch the effects each line of code. > The[ insertion of a break-point] allows the program to be stopped at a predetermined point in order to inspect its state.
38
Features of an IDE that the programmer would use when writing the code
> Auto Complete - can view identifiers or avoid spelling mistakes > Colour coding text or syntax highlighting - can identify features quickly or use to check code is correct > Stepping through a program – run one line at a time and check result > Breakpoints – stop the code at a set point to check value of variables > Variables watch/watch window - you can check that variables are storing the values that you intend
39
Array (data structure and why)
> Arrays are variables that have multiple elements, all of the same data type. > Arrays are a static data structure because the size of the array does not change when the program is running.
40
Lists (data structure and why)
Lists are dynamic data structures because the size of the list changes when the program is running.
41
The two main differences between tuples and 1D arrays?
> Firstly, tuples cannot be edited once they have been assigned > secondly, tuples can contain values of different data types unlike arrays where every item must be of the same type.
42
Record Data Structure
>A record is a data structure that allows items of data of different types to be stored together in a single file. >Data is stored in different fields and multiple records can be stored together in a file. >Not all programming languages support this structure.
43
Stacks
Stacks are ‘last in, first out’(LIFO) data structures.
44
Stack other information
We use one pointer: a top pointer. The top is often called the stack pointer. Data is added and removed from the top of the stack. We use the command PUSH to add data and POP to remove data. It is a dynamic data structure.
45
Algorithm to add an item to a stack
``` Procedure Push If StackPointer = Maximum Then print(“Stack is full”) Else NewItem=input(“Enter the item to add”) StackPointer = StackPointer + 1 Stack[StackPointer ] = NewItem Endif End Procedure ```
46
Algorithm to remove an item from a stack
``` Procedure Pop If StackPointer = Minimum Then print( “Stack is empty”) Else PoppedItem = Stack[stackPointer] StackPointer = StackPointer - 1 Endif End Procedure ```
47
queue
A queue is a ‘first in, first out’ (FIFO) structure.
48
Queue other information
We use two pointers: a start and an end. We also use enqueue and dequeue commands. It is a dynamic data structure.
49
Algorithm to add an item to a linear queue
``` procedure enqueue() size = 10 item = input("Enter item name") if tail == size - 1 then print("Queue is full") elseif (tail == -1) and (head == -1) Then tail =0 head = 0 queue[tail]=item else tail = tail + 1 queue[tail]=item endif End procedure ```
50
Algorithm to remove an item from a linear queue
``` procedure dequeue() if (tail == -1) and (head == -1) Then print(“Queue is empty") elseif (tail == head) Then print(queue[head]) head = -1 tail = -1 else print(queue[head]) head = head + 1 endif End procedure ```
51
Algorithm to add an item to a circular queue
``` Procedure enqueue Size=5 If (tail+1)MOD Size == head Then print(‘Queue is full’) ElseIf (tail == -1) and (head == -1) Then tail = 0 head = 0 Queue[tail] = NewItem Else tail = (tail + 1) MOD Size Queue[tail] = NewItem Endif End Proc ```
52
Algorithm to remove an item from a circular queue
``` Procedure dequeue size = 5 If (tail == -1) and (head == -1) Then Print (‘Queue is Empty’) Elseif (head == tail) Then RemovedItem = Queue[head] head = -1 tail = - 1 Else RemovedItem = Queue[head] head = (head + 1) MOD Size Endif End Proc ```
53
bubble sort code
``` swap=True size=length(nlist) - 1 While (swap == True) swap = False FOR index= 0 to size if nlist[index] > nlist[index + 1] then swap = True temp = nlist[index] nlist[index] = nlist[index + 1] nlist[index + 1] = temp endif next index size =size - 1 endwhile ```
54
PSEUDO CODE FOR INSERTION-SORT
``` Procedure insertionSort(alist:ByRef) FOR index = 0 to length(alist) -1 currentvalue = alist [index] position = index WHILE (position > 0 AND alist[position -1] > currentvalue) alist[position]= alist[position-1] position = position -1 alist[position] = currentvalue ENDWHILE NEXT index End Procedure ```
55
What is Linear Search
This is the simplest searching method. It is also called the linear search or sequential search.
56
Linear Search pseudocode
``` A datastore is created (list, array etc) SearchItem = Input(“Enter the item you are looking for: “) FOUND = False Index=0 While FOUND = False and index ```
57
linear search as a function
``` Function finditem(givenitem) FOUND = False Index=0 While index ```
58
What is Binary Search
This is a fast method of searching for an item in a sorted long list.
59
Binary search pseudocode
``` A datastore is created (list, array etc) If the List is not in order, a sorting algorithm is applied SearchItem = Input(“Enter the item you are looking for: “) FOUND = False Lower = 0 Upper = Length(List) – 1 (Last item in the list) While FOUND = False and Lower <= Upper MiddlePosition = (Lower + Upper) DIV 2 IF List[MiddlePosition] < SearchItem Then Lower = MiddlePostition + 1 ELSE IF List[MiddlePosition] > SearchItem Then Upper = MiddlePostition – 1 ELSE FOUND = True ENDIF ENDIF End While IF Found = True Then Print the item found Else Print the item is not in the list End If ```