Further Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Further Programming

Programming Paradigms
 A programming paradigm defines the style or model followed when
programming.
o Low-Level Programming
 Machine code (binary – lowest level) or Assembly language
 “Low” refers to the small/non-existent amount of abstraction
between the language and machine language
 Instructions can be converted to machine code without a
compiler or interpreter
 The resulting code runs directly on the specific computer
processor, with a small memory footprint
 Programs written in low-level languages tend to be relatively
non-portable – code written for a Windows processor might not
work on a Mac processor
 Simple language, but considered challenging to use due to
numerous technical details that the programmer must
remember.
o Imperative (Procedural) Programming
 It uses a sequence of statements to determine how to reach a
specific goal. These statements are said to change the
program's state as each one is executed in turn.

 Each statement changes the program's state, from assigning


values to each variable to the final addition of those values.
Using a sequence of five statements, the program is explicitly
told how to add the numbers 5, 10, and 15 together.
o Object-Oriented Programming (OOP)
 An extension of imperative programming.
The focus is on grouping functions and data into logical classes
and instances of classes called objects.
 Object-oriented programming is further explained in its section
later in the notes.
o Declarative Programming
 Non-procedural and very high level (4th generation)
 Control flow is implicit, not explicit, like Imperative Programming
 The programmer states only what needs to be done and what
the result should look like, not how to obtain it.
 A vital feature → backtracking – where a search goes partially
back on itself if it fails to find a complete match the first time
around
 Goal – a statement we are trying to prove, either true or false,
effectively forms a query.
 Instantiation – giving a value to a variable in a statement
 A Declarative Language is further explained with examples in
its section in the “theory” section of the notes.
File Processing and Exception Handling
File Processing
 Records are user-defined data structures
Defining a record structure for a Customer record with relevant fields (e.g.,
customer ID) in Python:
 Files are needed to import contents (from a file) saved in secondary memory
into the program or to save the output of a program (in a file) into secondary
memory so that it is available for future use.
Pseudocode:
 Opening a file:
OPENFILE <filename> FOR READ/WRITE/APPEND
 Reading a file:
READFILE <filename>
 Writing a line of text to the file:
WRITEFILE <filename>, <string>
 Closing a file:
CLOSEFILE
 Testing for the end of the file:
EOF()
Python:
 Opening a file
variable = open(“filename”, “mode”)
Where the mode can be:

Mod
Description
e

It opens a file for reading only. The pointer is placed at the beginning
r
of the file.

It opens a file for writing only. Overwrites file if file exists or creates a
w
new file if it doesn’t

Opens a file for appending. Pointer at the end of the file if it exists or
a
creates a new file if not
 Reading a file:
o Read all characters
variable.read()
o Read each line and store it as a list
variable.readlines()
 Writing to a file:
o Write a fixed sequence of characters to file
variable.write(“Text”)
o Write a list of strings to file
variable.write[“line1”, “line2”, “line3”]
 Using direct access or Random File allows us to read records directly.
‘random’ is misleading since records are still systematically read from and
written to the file.
Pseudocode:
 Opening a file using the RANDOM file mode, where once the file has been
opened, we can read and write as many times as we would like in the same
session:
OPENFILE <filename> FOR RANDOM
 Move a pointer to the disk address for the record before Reading/writing to a
file can occur:
SEEK <filename>, <address>
Each record is given an ‘address’ at which it is to be written – the record key.
 Write a record to the file:
PUTRECORD <filename>, <identifier>
 Read a record from a file:
GETRECORD <filename>, <identifier>
 Close the file:
CLOSE <filename>
Algorithms for File Processing Operations for Serial and Sequential Files:
 Display all records:

 Search for a record:


*Special Case: If the records in a sequential file are of a fixed length, a record can
be retrieved using its relative position. So, the start position in the file could be
calculated for the record with the key number 15, for example.
 Add a new record – Serial Organisation:

 Add a new record – Sequential Organisation:


*Some file processing tasks, like this one, require two files because serial/sequential files
can only be opened to read from or write to in the same session.*

 Delete a record:
 Amend an existing record:

Python example of Sequential File Handling:


Algorithms for File Processing Operations for Random Files:
 Display all records:

 Add a new record:

Python:

 Delete a record:
 Amend an existing record:

 Search for a record:


Python:

Python example of Random File Handling:


Exception Handling
 An exception is a runtime error/ fatal error/situation which causes a program
to terminate/crash.
 Exception-handling – code that is called when a run-time error or “exception”
occurs to prevent the program from crashing
 When an exception occurs, we say it has been “raised.” You can “handle” the
exception raised by using a try block.
 A corresponding except block “catches” the exception and returns a message
to the user if an exception occurs.
e.g.

Use of Development Tools and Programming


Environments
 Integrated Development Environment: an application that provides
several tools for software development. An IDE usually includes: a Source
code editor, debugger and automated builder
 Features in editors that benefit programming:
o Syntax Highlighting: keywords are coloured differently according to
their category
o Automatic indentation: after colons, for example, to make code
blocks more distinct, allowing for better code readability
o A library of preprogrammed subroutines that can be implemented
into a new program to speed up the development process

Compiler Interpreter

Translates source code (e.g. Python


Directly executes/performs instructions
code) into machine code, which can
written in a programming language by
be run and executed by the
translating one statement at a time.
computer

It takes significant time to analyze


the source code, but the overall It takes less time to analyze the source
execution time is comparatively code, but the execution time is slower.
faster.

Generates intermediate object code, No intermediate object code is


Compiler Interpreter

which further requires linking and generated; hence, it is memory


more memory. efficient.

It generates the error message only


Continues translating the program until
after scanning the whole program.
the first error is met, in which case it
Hence, debugging is comparatively
stops. Hence, debugging is easy.
complex.

Programming languages like C and Programming languages like Python


C++ use compilers. and Ruby use interpreters.
 Systems that require high performance and for the long run should be
written in compiled languages like C, C++,
 Systems that need to be created quickly and easily should be written in
interpreted languages
Features available in debuggers:
 Stepping - traces through each line of code and steps into procedures.
Allows you to view the effect of each statement on variables
 Breakpoints - set within code; program stops temporarily to check that it is
operating correctly up to that point
 Go to File/Line - Look at the current line. Use the cursor and the line above
for a filename and line number. If found, open the file if not already open,
and show the line. Use this to view source lines referenced in an exception
traceback and lines found by Find in Files. Also available in the context menu
of the Shell window and Output windows.
 Debugger (toggle) - When active, code entered in the Shell or run from an
Editor will run under the debugger. In the Editor, breakpoints can be set with
the context menu. This feature is still incomplete and somewhat
experimental.
 Stack Viewer - Show the stack traceback of the last exception in a tree
widget with access to local and global variables.
 Auto-open Stack Viewer - Toggle automatically opening the stack viewer on
an unhandled exception.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy