Computer Programming Notes

What is a Computer?

  • A device capable of performing computations and making logical decisions at high speeds.

  • Processes data under the control of computer programs.

  • Hardware includes a processing unit, memory, and I/O devices.

  • Software refers to the computer programs that run on the hardware.

Hardware Aspects

Computer Organization

  • Input/Output unit:

    • Monitor

    • Pointing device and Keyboard

    • Secondary storage (hard disks)

  • Memory unit:

    • Read-only memory (ROM)

    • Read-Write memory (RAM)

  • Central Processing Unit (CPU):

    • Arithmetic & Logic Unit (ALU)

    • Internal registers

Basic Diagram of Computer Architecture

  • Includes:

    • Address Bus

    • Memory

    • CPU

    • Input

    • Output

    • Real World

    • Data Bus

    • Control Bus

Input/Output (I/O) Devices

  • Functions:

    • Input Devices: Keyboards, mice, scanners, and microphones allow users to input data into the computer.

    • Output Devices: Monitors, printers, and speakers output data from the computer to the user.

  • Collaboration:

    • Input devices send data to the CPU for processing.

    • The CPU sends the results to output devices.

    • The CPU communicates with I/O devices through interfaces and protocols.

Memory Unit

  • Functions:

    • Storing Data: Stores data and instructions.

      • Includes volatile (RAM) and non-volatile memory (ROM, hard drives, SSDs).

    • Temporary Storage: RAM provides space for data and instructions currently in use.

      • Faster, volatile (loses data when power is off).

    • Permanent Storage: ROM stores firmware and boot instructions.

      • Storage devices (HDDs, SSDs) store data and programs permanently.

  • Collaboration:

    • Provides the processor with data and instructions.

    • The CPU continuously reads from and writes to memory.

Processor (Central Processing Unit, CPU)

  • Functions:

    • Executing Instructions: Fetches, decodes, and executes instructions.

      • Performs arithmetic, logical, control, and I/O operations.

    • Control Unit (CU): Directs the operation of the processor.

      • Tells memory, ALU, and I/O devices how to respond.

    • Arithmetic Logic Unit (ALU): Performs arithmetic and logical operations.

  • Collaboration:

    • Fetches instructions from memory, decodes them, and executes them.

    • Communicates with memory to retrieve and store data.

Collaboration to Complete Tasks

  • Task Initiation: User inputs data via an input device.

  • Data Processing: CPU fetches instructions and data from memory and processes the data.

  • Intermediate Storage: CPU stores intermediate results in RAM.

  • Output Generation: CPU sends final results to an output device.

  • Data Management: CPU saves processed data to permanent storage.

Example: Opening a Word Processor

  • User Input: User clicks on the word processor icon (mouse).

  • Fetching Instructions: CPU fetches instructions from storage (HDD/SSD) into RAM.

  • Execution: CPU executes the instructions to load the word processor.

  • User Interaction: User types (keyboard); CPU processes keystrokes and stores in RAM.

  • Saving Data: CPU writes data from RAM to storage (HDD/SSD).

  • Output: Document is displayed on the monitor.

Operating System

  • A layer of software that manages the computer's hardware.

  • Includes the file system.

  • Loaded from secondary storage at startup.

  • Examples: MS Windows, UNIX, Linux, MAC OS X.

Main Functions of an Operating System

  • Hardware management

  • File system management

  • Provides a user interface

  • Executes and provides services for application software

  • Data access

  • System Resource management (e.g., memory)

  • System security (access control)

  • Network communications (share resources)

Computer Configurations

  • Hardware configurations:

    • Stand-alone Personal Computing (generic PC): Desktops, Laptops, Tablets & Smartphones

    • Workstation: CAD WS, Media WS, Scientific WS

    • Server: File, Print, DB, Web, Mail, Gaming, Media, Backup, Access Control, etc.

    • Tablets and Smartphones

    • Embedded systems: IoT devices, inside appliances

    • Local Area Network (LAN)

    • Virtual machines (vmware, hyper-V)

  • Software configurations:

    • Stand Alone programs

    • Client/Server applications

    • Distributed Computing

    • Web Services

    • Cloud Computing

  • Future: Quantum Computers

Software Aspects

Programming Languages

  • Machine language:

    • Lowest level language.

    • The only language computers understand.

  • Assembly language:

    • Higher level than machine language.

    • Needs to be assembled to machine language.

  • High-level language:

    • More understandable by humans.

    • Needs to be compiled for computers to understand.

Machine Language

  • Defined by the computer hardware design.

  • Machine dependent.

  • The only code that computers understand.

  • Cumbersome to work with.

  • Ultimately reduced to 1’s and 0’s.

  • Example:

    • +1300042774+1300042774

    • +1400593419+1400593419

    • +1200274027+1200274027

Assembly Language

  • Uses English-like abbreviations.

  • More readily understandable to humans.

  • Assemblers needed to translate assembly code to machine language.

  • Cleaner to work with but still requires many instructions.

  • Example:

    • LOAD BASEPAY ADD OVERPAY STORE GROSSPAY

High-Level Language

  • Instructions look like everyday English phrases and contain mathematical notations.

  • Single statements accomplish substantial tasks.

  • Compilers and Interpreters translate high-level languages to machine language.

  • Example code: grossPay = basePay + overTimePay

  • Example languages: Fortran, Cobol, Pascal, Java, C, C++, Visual Basic, C#.

Code Translators

  • Assemblers:

    • Translates assembly code into machine language.

  • Compilers:

    • Converts HLL code into machine language.

    • Requires recompilation after code changes.

  • Interpreters:

    • Provide real-time compilation of HLL source code.

    • Popular for frequent updates and corrections.

    • Slower execution than compiled programs.

VISUAL BASIC FOR APPLICATIONS (VBA)

VBA for Excel

  • GUI radically changed how engineers interact with computers.

  • Excel program can be viewed as a GUI.

  • Facilitates data entry and produces charts using Visual Basic for Applications (VBA).

VBA Offers:

  • Numerical programming functionality:

    • Loops, arrays, logical structures, etc

  • Object-oriented programming features

  • Easy way to send information to and from Excel worksheets

Basic Concepts

  • Programs

  • Assignment

  • Decisions and Loops

The “idea” of a Program

  • A program is a sequence of unambiguous instructions (statements).

  • The computer implements instructions sequentially at a fast rate.

  • Ambiguous and illogical instructions won't work properly.

Example Program in VBA

  • vba Sub Adder() a = 10 b = 33 c = a + b End Sub

  • Assigns numeric constants to variables a and b.

  • Adds them and assigns the result to variable c.

The Concept of Assignment

  • Information is represented in two ways:

    • Directly as constants: a value that does not change.

    • Symbolically as variables: a symbolic name that can take on different values.

  • Constants can be assigned to variables.

Assignment Statement & Computer Memory

  • A variable is a label the computer associates with a memory location.

The Assignment Operator (=)

  • The equals sign means “is replaced by” or “is assigned the result”.

  • Different from the conventional meaning of equality.

  • Alternatives: angle  45, Pascal and MathCad adopted “:=”.

  • Assignment statements must have a single variable on the left-hand side.

  • Invalid VBA statements: a + b = c, 3 = x – y + 99

  • Valid VBA statement: x = x + 1

  • After execution, the memory location for x increments by one.

Alphanumerical Information

  • Computers also process information other than numbers.

  • Handle names, labels, and identification numbers (strings).

  • String constants are enclosed in quotes.

  • Variables storing strings are called string variables.

  • Example: n = “Louis Armstrong” sets up a memory location n containing “Louis Armstrong”.

Decisions and Loops

  • Ways to deviate a computer program from its sequential mode of operation.

  • Decisions: If Then Else

  • Loop: For Next

Decisions

  • Implemented using the If-Then statement.

  • Example:

    • vba If a < 0 Then s = -1 Else s = 1 End if

Loops

  • A loop repeats a VBA statement(s) several times.

  • The For-Next loop is the simplest way to do this.

  • Example:

    • vba x = 0 n = 5 For i = 1 to n x = x + i Next i

Example: Summation or Factorial

  • vba calctype = “summation” n = 5 If calctype = “summation” Then x = 0 For i = 1 to n x = x + i Next I Else x = 1 For i = 1 to n x = x * i Next i End if

  • Illustrates how sequences, decisions, and loops can be combined.

Summary

  • What is a computer?

  • Computer organisation

  • Operating system

  • Computer configurations

  • Programming languages

  • Code translators

  • VBA for Excel

  • Basic Concepts

  • The idea of a program

  • Assignment & Memory

  • Assignment operator (=)

  • Alphanumeric information

  • Decisions & Loops

  • Example

Introduction to Computer Programming Discussion