Foundational Concepts in Computer Programming and Languages

Foundational Concepts in Computer Programming

  • Objectives of Programming Study:

    • Define terms commonly used in computer programming.

    • Classify and differentiate programming languages.

    • Describe the purpose of programming.

    • Explain the basic algorithm followed in computer programs.

    • Describe techniques adopted for program development.

  • Definition of a Program: A sequence of instructions expressed in standard form that specifies how to perform a computation, which can be mathematical or symbolic.

  • Definition of a Programming Language:

    • A standardized communication technique to express an instruction or computation in a form understood by a computer.

    • A tool that enables a programmer to precisely specify what data a computer will act upon, how these data will be stored or transmitted, and precisely what actions to take under various circumstances.

  • Definition of Programming: The disciplined approach in writing codes or programs.

  • Commonality Among Languages: Programming languages differ in commands (functions) used and syntax (for example, the specific function to display text on the console differs across languages), but all programming languages share fundamental elements of programming.

Classification of Programming Languages

  • General-Purpose Programming Languages: Languages used to build diverse software across various computing domains.

    • Python: A general-purpose language among thousands available. It is well-suited for data analysis, artificial intelligence, scientific computing, and backend web development.

    • Java: A general-purpose language, identified in surveys as one of the most popular languages, which is effective for client-server web applications and mobile applications.

  • Level-Based Classifications: Programming languages are categorized into low-level and high-level languages based on their proximity to hardware execution.

    • Low-Level Languages: Computer programs written in the native language of the computer that can be directly executed by the processor. They are tied directly to specific hardware.

    • Machine-Level Language (Machine Code): The lowest level of programming language. It is written entirely in numbers and is directly executed by the computer's processor without undergoing translation.

    • Assembly Language: A low-level programming language that uses mnemonics or English words to represent machine code instructions. Assembly code must be translated into machine code prior to processor execution.

    • High-Level Languages: Programming languages that closely resemble the English language, making them easier for programmers to read, write, and understand.

    • Portability: Compatible with different hardware architectures (can run across multiple platforms).

    • Translation Requirement: High-level code must be translated into machine-level language for execution.

    • Advantages Over Low-Level Languages:

      • Much easier to program.

      • Takes less time to write programs.

      • Programs are shorter and easier to read.

      • Highly portable across distinct platforms.

Classification of Program Translators

  • Program Translators: Tools used to convert high-level source code into machine-level language for processor execution.

  • Interpreter:

    • Execution Mechanism: Processes programs one line at a time, alternately reading lines and performing computations.

    • Workflow Pipeline: Source Code→Interpreter→Output\text{Source Code} \rightarrow \text{Interpreter} \rightarrow \text{Output}

  • Compiler:

    • Execution Mechanism: Processes programs by translating the entire program first before executing the code and performing computations.

    • Object Code Creation: Translating the complete program generates a file called the object code, which contains machine-level code.

    • Workflow Pipeline: Source Code→Compiler→Object Code→Executor→Output\text{Source Code} \rightarrow \text{Compiler} \rightarrow \text{Object Code} \rightarrow \text{Executor} \rightarrow \text{Output}

Coding Organization Models

  • Procedural (Structured) Programming:

    • A disciplined approach to program development producing code that is clear, demonstrably correct, and easy to modify.

    • Focuses on writing functions, procedures, methods, or sub-routines.

    • Function Definition: A section of organized code that performs a specific task.

    • Program Structure: Programs are composed of built-in functions supplied by the language alongside user-defined functions, avoiding the need to write programs entirely from scratch.

    • Syntax Indicator: Functions are universally followed by an open and close parenthesis: ().

  • Object-Oriented Programming (OOP):

    • Focuses on object technology, reflecting the human perception of a world made of objects.

    • Object Characteristics: Objects are defined by their properties (attributes) and behaviors (actions).

    • Focus of Programmers: Writing object (class) definitions rather than individual functions.

    • Class Definition: Specifies the general format of its objects, establishing the properties and actions available to an object.

    • Stated Advantages:

    • Code reusability.

    • Better code organization and understandability.

    • Fewer maintenance requirements.

Basic Elements of Programming Languages

  • Core Universal Elements: Seven essential constructs common to all programming languages:

    1. Data: Values manipulated by codes, which can exist as constants or variables.

    2. Input: Data manipulated by the program, received from a keyboard, file, or hardware device.

    3. Output: Data displayed on the screen, sent to a file, printer, or network.

    4. Operation: Computations executed by code, which include:

    • Mathematical Computations: Examples include solving a system of equations or finding the roots of a polynomial.

    • Symbolic Computations: Examples include searching and replacing text in a document.

    1. Sequence: The inherent flow of execution of codes, including possible paths of execution.

    2. Condition / Selection: Mechanics allowing a program to evaluate a specific condition and execute the corresponding sequence of statements.

    3. Repetition / Iteration / Loop: Mechanics allowing a section of code to repeat for a predefined number of times or until specific criteria are met.

The Problem-Solving Process

  • Systematic Four-Step Workflow:

    1. Formulating the Problem (Problem Analysis):

    • Clearly defining the problem in terms of its input and output requirements.

    • Every computing problem involves inputs, computations applied to manipulate the input, and desired outputs.

    1. Creatively Thinking About Solutions (Flowcharting / Pseudo-coding):

    • Algorithm: A general step-by-step process or solution for solving a specific computing problem.

    • Flowchart: A diagram containing standardized symbols showing the sequence of steps to solve a problem. Each symbol represents a specific function or action.

    • Pseudo-code: A sequential set of written plain-language statements used to solve a computing problem.

    1. Expressing the Solution Clearly and Accurately (Code Implementation):

    • Writing the algorithm using formal notation in a specific programming language.

    • Requires complete knowledge of the syntax (grammatical structure) and semantics (meaning) of the chosen language.

    1. Testing the Solution (Debugging):

    • Identifying and correcting errors committed during analysis or implementation.

    • After debugging, the process may loop back to problem analysis to integrate new features, or end with final deployment.

Program Development Approaches and Error Handling

  • Incremental Development Technique:

    • A program development method designed to avoid lengthy debugging sessions by integrating and testing only a small section of code at a time.

    • Maintains a working program at every step by starting with a small functioning section of code and adding small modifications incrementally.

  • Etymology of Debugging: Derived from "bug" (programming errors). Debugging is the process of tracking, troubleshooting, and correcting errors in programs.

  • Classifications of Programming Errors:

    1. Syntax Error:

    • Violations of program structure and rules governing grammatical constructs.

    • Easy to identify and fix because compilers and interpreters detect them and output explicit error messages.

    1. Runtime Error (Exceptions):

    • Errors that do not appear until program execution.

    • Detected by the compiler or interpreter during execution, producing error messages.

    • Example: Division by zero.

    • Execution Mechanics: In a script containing function a()a() (which includes a division by zero error) and function b()b(), if the final code line invokes only function b()b(), the script will execute and output successfully without crashing. Python never encounters the runtime error because function a()a() was never called.

    1. Semantic Error:

    • The most difficult bug to track because no error messages are generated.

    • Pertains to the logic or meaning of the program.

    • Occurs when the computer executes instructions as written, but the output fails to match expected results due to flawed logic.

  • Iterative Debugging Technique (Sherlock Holmes Method):

    1. Identify clues about the error.

    2. Create a hypothesis regarding what went wrong.

    3. Make a small, targeted modification in the program based on the hypothesis.

    4. Test the program for correctness.

    5. If the hypothesis is incorrect, formulate a new hypothesis and repeat the procedure.

Program Algorithm Models

  • Basic Algorithm (Input-Process-Output / I-P-O Model):

    • Input Step: Acquires data via keyboard input, database/file retrieval, or embedded data (data hardcoded directly within the source code).

    • Process Step: Executes one or more mathematical or symbolic operations.

    • Output Step: Displays results via a screen monitor, printer, or external output.

    • Step Overlap: Input and process steps, or process and output steps, may overlap. Processing can range from a single operation to complex statement sets involving sub-inputs and sub-processes.

  • Variations of Sequence Models:

    • Model 1: Input→Process→Output\text{Input} \rightarrow \text{Process} \rightarrow \text{Output}

    • Model 2: Process→Output\text{Process} \rightarrow \text{Output}

    • Model 3: Input→Process\text{Input} \rightarrow \text{Process}

    • Model 4: Input→Process→Input→Process→Output\text{Input} \rightarrow \text{Process} \rightarrow \text{Input} \rightarrow \text{Process} \rightarrow \text{Output}

Glossary of Technical Terms

  • Artificial Intelligence: The intelligence exhibited by a machine in order to emulate human ways of thinking.

  • Client-Server: A network structure where one or more computers act as a server (provider of resources or services) and other computers act as a client (requester of services).

  • Data Analysis: The process of transforming data into a model that aims to extract useful information.

  • Encapsulation: A characteristic of a program structure which restricts access to certain components of the code.

  • General-Purpose Language: A programming language which can be used for different applications ranging from desktop applications, to web and mobile applications, and even embedded systems.

  • Scientific Computing: A multidisciplinary process of using advanced computational capabilities to analyze and solve complex problems.

  • Platform: A hardware or software environment upon which a piece of software is built.

  • Source Code: The set of instructions written in plain text.