KEMBAR78
Module 3 | PDF | Object Oriented Programming | Control Flow
0% found this document useful (0 votes)
5 views17 pages

Module 3

Uploaded by

Abdul Basit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Module 3

Uploaded by

Abdul Basit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Topic 1: Interpreted & Compiled Programming Languages

Programming Languages Overview

 Purpose: Allow humans to communicate instructions to computers.


 Computers' native language: Machine code (binary: 1s and 0s).
 Human-readable code: Programming languages make it easier to write instructions.
 Two main categories:
1. Interpreted languages
2. Compiled languages
 Language choice often depends on the project’s purpose.

Interpreted Programming Languages

 Also called: Scripted or scripting languages.


 Execution process:
1. Written in human-readable code.
2. Interpreter translates the code into machine code at runtime.
3. Computer executes the task.
 Where they run:

o Through an interpreter on the computer’s OS.


o Inside a web browser.
 Key characteristics:
o Require an interpreter to execute.
o Often easier to learn and versatile.
o May become outdated as tech changes.
 Examples:
o JavaScript – Browser-based scripting language.
o Python – Easy to learn; popular for many purposes.
o Lua – Lightweight; common in game development.
o HTML – Markup language for web pages.
 Interpreters: Either built into browsers or installed as separate programs.

Compiled Programming Languages

 Purpose: Used for more complex programs and larger tasks.


 Execution process:
1. Source code → compiled by a compiler into machine code.
2. Produces a single executable file (.exe, .app, etc.).
3. Program runs directly on the device without re-translation.
 Advantages:

o Faster execution (machine code is ready to run).


o Can run repeatedly without reinterpreting code.
 Common uses:
o Operating systems.
o Large-scale applications.
 Examples:
o C, C++, C# – Used in Windows, macOS, Linux.
o Java – Platform-independent; used for Android OS.
 Clarification: Java ≠ JavaScript (Java is compiled, JavaScript is interpreted).

Key Takeaways

 Interpreted languages:
o Run scripts that may be repetitive.
o More versatile; work across platforms with the right interpreter.
o Examples: JavaScript, Python, HTML.
 Compiled languages:
o Create standalone executables.
o Used for complex, performance-intensive applications.
o Examples: C, C++, Java.
 Execution difference:
o Interpreted → Translated at runtime, slower execution.
o Compiled → Pre-translated into machine code, faster execution.
Topic 2: Comparing Compiled & Interpreted Programming
Languages
Comparing Compiled and Interpreted Programming Languages

How Developers Choose a Language

 Choice depends on:


o Developer’s experience and trust in the language.
o User needs and requirements.
o Efficiency for the intended task.

Interpreted Programming Languages

 Purpose: Create scripted source code for smaller, repetitive tasks.


 Execution Process:
o Source code is executed line-by-line by an interpreter.
o Interpreter may be built into the OS or a web browser.
 Key Traits:
o Also called script code or scripting languages.
o Needs the interpreter each time the program runs.
o Works across many operating systems if the correct interpreter is available.
o Easier to learn than compiled languages.
o Best for websites and smaller repeated processes.
 Example Use Case:
o Retrieving and displaying last month’s webpage views in a table for a client.
 Examples:

1. Python – General-purpose, case-sensitive, works on Windows/macOS/Linux,


requires Python engine.
2. JavaScript – Client-side scripting in browsers, case-sensitive, runs with HTML
or as separate files.
3. HTML – Hypertext Markup Language, mostly case-insensitive, formats web
pages in browsers.

Compiled Programming Languages

 Purpose: Create executable files for larger, complex programs.


 Execution Process:
o Code is converted to machine code and compiled into one executable file.
Once compiled, it runs faster and doesn’t require re-translation every run.
o
 Key Traits:
o Also referred to simply as programming languages.
o Harder to learn and more complex than interpreted languages.
o Typically used for larger applications, like operating systems.
o Platform-specific — compiled for a specific OS.
 Example Use Case:
o Building a spreadsheet application like Microsoft Excel to manage client data.
 Examples:

1. C – Basis for Windows and other OS, case-sensitive, fast, requires less memory.
2. C++ / C# – Variations of C with additional features.
3. Java – Compiled, case-sensitive, object-oriented, runs on JVM, cross-platform
(Windows/macOS/Linux), primary language for Android OS.

Major Differences

Feature Interpreted Languages Compiled Languages

Execution Line-by-line via interpreter Compiled into executable file

Speed Slower Faster

Learning Curve Easier Harder

Portability Cross-platform with interpreter Platform-specific

Best For Websites, repetitive tasks Large apps, OS

Examples Python, JavaScript, HTML C, C++, C#, Java

Key Takeaways

 Interpreted:
o Flexible, cross-platform with correct interpreter.
o Ideal for smaller, repetitive, web-related tasks.
 Compiled:
o More efficient for large programs.
o Faster execution and platform-specific.
 Examples:
o Interpreted: Python, JavaScript, HTML
o Compiled: C, C++, C#, Java
Topic 3: Query & Assembly Programming Languages
Query and Assembly Programming Languages – Notes

1. Classification of Programming Languages

 High-level languages:
o More sophisticated, closer to human language (often English-based).
o Easier to understand, faster to code and debug.
o Examples:
 Query languages (SQL)
 Structured programming languages (Pascal)
 Object-oriented languages (Python)
 Low-level languages:
o Use symbols to represent machine code (binary).
o Closer to hardware.
o Example: Assembly languages (ARM, MIPS, X86)

2. Query Languages

 Definition: A query is a request for information from a database.


 Function:
o Database receives query → searches tables → returns results.
o Both the application and database must use the same query language.
 Purpose: To request data or perform CRUD operations:
o Create
o Read
o Update
o Delete
 Key example:
o SQL – most common query language for database queries/management.
 Other query languages: AQL, CQL, Datalog, DMX.
 SQL vs NoSQL Databases:
o SQL: Relational, structured, predefined schemas.
o NoSQL: Non-relational, dynamic schemas for unstructured data.
 Types of SQL Statements:
o Select queries → Retrieve data.
o Action queries → Manipulate data (CREATE, INSERT, UPDATE).
o Can also manage admin functions (create users, modify permissions).
 Data structure:
o Databases use structured tables (rows + columns).
o Queries extract relevant data into ordered results.
3. Assembly Languages

 Definition: Low-level language using symbols to represent binary machine code.


 Relation to hardware:
o Closely tied to processor architecture (e.g., ARM, MIPS, X86).
o Each CPU type has its own assembly language.
 Syntax:
o One statement per line.
o Format:

[Label] Instruction [Operands] [;Comment]

 Instruction = mnemonic (e.g., LDA, ADD)


 Operands = parameters (where data is located)
 Comments optional.
 Translation:
o Uses an assembler (not compiler or interpreter).
o One-to-one translation: each statement → one machine code instruction.
 Examples of mnemonics/opcodes:
o INP (Input), OUT (Output), LDA (Load), STA (Store), ADD (Add)
 Execution:
o Opcodes → tell processor what to do.
o Operands → tell processor where to find data.

4. Key Takeaways

 High-level: Query languages (SQL), structured (Pascal), object-oriented (Python).


 Low-level: Assembly languages tied to CPU architecture.
 Query language: Requests/manipulates data in DB, supports CRUD.
 SQL is most common; supports SELECT (retrieve) and action queries (manipulate).
 Assembly: Symbolic, hardware-specific, assembler-translated, one statement = one
instruction.
Topic 4: Understanding Code Organization Methods
Understanding Code Organization Methods – Notes

Importance of Code Organization

 Organizing code improves readability, maintainability, and scalability.


 Well-planned software design:
o Helps programmers write cleaner, more reliable code.
o Reduces bugs and errors throughout a project's life.
 Visual planning before coding:
o Improves code quality.
o Provides a consistent, logical format during coding.

Two Main Methods of Code Organization

1. Flowcharts
2. Pseudocode

Flowcharts

 Definition:
Pictorial representation of an algorithm showing steps as boxes connected by arrows
(indicating sequence).
 Purpose:
Analyze different methods for solving a problem or completing a process.
 Advantages:
o Easy for small concepts/problems.
o Good communication tool for logic.
o Easier to create in early project stages than pseudocode.
 Common Flowchart Symbols:
o Start/End: Capsule shape
o Process: Rectangle
o Decision: Diamond
o Data: Parallelogram
o Connectors: Arrows
 Example: Adding two numbers:

1. Start →
2. Input n1, n2 →
3. Sum = n1 + n2 →
4. Print Sum →
5. End
 Flowchart Software:

o Microsoft Visio
o Lucidchart
o Draw.io
o DrawAnywhere

Pseudocode

 Definition:
Informal programming description without strict syntax or tech details.
 Purpose:
o Outline program logic before writing actual code.
o Acts as a bridge between human thinking and executable code.
 Advantages:
o Good for larger projects.
o Language-independent.
o Helps beginners focus on logic instead of syntax.
o Easier for non-programmers to understand.
o Quick to modify and usually short (less than one page).
o Allows collaboration across different programming languages.
 Example:
Checking if a number is odd or even.
 Key Benefits Over Flowcharts:
o More concise.
o Explains exactly what each line of code does.
o Focuses on logic → easier development stage.
o Easier review by different teams.

Key Takeaways

 Organizing code is essential for quality, consistency, and collaboration.


 Flowcharts:
Best for visualizing small-scale processes, communication, and early project stages.
 Pseudocode:
Best for larger projects, cross-team collaboration, and focusing on logic over syntax.
 Both methods help reduce complexity, errors, and development time.
Topic 5: Insiders' Viewpoint: Types of Languages
Compiled vs Interpreted Languages — Experts’ Perspectives

 General consensus:
o For most development tasks, the choice between compiled and interpreted
languages doesn’t significantly impact the development or deployment process.
o Often comes down to personal preference and development speed.
 When it matters:
o In scenarios where small incremental performance gains are important.
o Compiled languages can provide more predictable performance since code is
pre-compiled.
o Example: performance-critical systems or environments requiring guaranteed
behavior on deployment.
 Preferences expressed:
o Compiled language preference:
 Offers more assurance that code will do what was intended.
 Less risk of deployment issues due to pre-compilation checks.
o Interpreted language preference:
 Offers flexibility and creativity in coding.
 Allows interesting runtime possibilities.
 Can adapt or modify behavior without recompiling.
 Risk: Potentially buggy code can be released more easily.
 Bottom line:
o Choice depends on:
 Development environment
 Tolerance for runtime errors
 Performance needs

Object-Oriented Programming (OOP) vs Procedural Programming

Benefits of OOP

 Conceptual model:
o Anything can be represented as an object.
o Allows for hierarchies and design patterns that make system structure easier to
understand.
o Makes it easier to visualize how different components interact.
 Advantages:
o Well-suited to real-world mappings and data models.
 Example: A library system with objects like Library, Book, Checkout.
o Helps beginners transition from thinking about physical objects to software
abstractions.
o Can improve code maintainability through modularity and encapsulation.
 Potential drawbacks:
o Overuse can lead to too much structure → excessive boilerplate code.
o Need to balance between structure and flexibility.
o Risk of being overly prescriptive in design, reducing adaptability.
 Best practice:
o A few simple, well-designed objects can greatly benefit a system without
overcomplicating it.

Benefits of Procedural Programming

 Feels more mathematical and pure engineering-focused.


 Provides a sense of being “inside the machine” — working closely with how the
computer executes tasks.
 Can be simpler for straightforward, sequential processes.
 Good for developers who think in terms of step-by-step execution rather than object
interactions.

Key Takeaways

 Compiled vs Interpreted:
o Mostly personal preference unless performance or error handling requirements are
strict.
o Compiled → more predictable, safer deployments.
o Interpreted → more flexible, dynamic runtime possibilities.
 OOP vs Procedural:
o OOP → models real-world concepts, helps with system organization, but can be
overengineered.
o Procedural → simpler, more mathematical, direct control of execution flow.
o Best choice depends on project requirements, team preferences, and desired
balance between structure and flexibility.
Topic 6: Branching & Looping Programming
Branching and Looping Programming Logic

1. Overview

 Two major types of programming logic:


1. Branching – Deciding what action to take.
2. Looping – Deciding how many times to perform an action.
 Common foundation: Both use Boolean expressions and variables.

2. Boolean Expressions & Variables

 Boolean expression: A statement that evaluates to true or false.


 Variables:
o Have assigned values.
o Passed into functions or subroutines.
o Values can change based on program conditions or inputs.
 Role: Boolean logic + variables = decision-making basis for programs.

3. Branching Logic

 Definition: The program chooses between multiple paths of execution based on


conditions.
 How it works:
o Conditions are evaluated.
o Program follows the branch corresponding to the condition’s result.
o Parameters can be user input or results from previous code.
 Key branching constructs:

1. if – Executes code if condition is true; otherwise, does nothing.


2. if-then-else – Executes “then” block if condition is true, otherwise executes
“else” block.
3. switch – Selects execution path based on the value of a variable or expression.
4. GoTo – Jumps to another line of code (unlike function calls, control does not
return).
 Note: Unlimited branching is possible for complex logic.

4. Looping Logic
 Definition: Repeats a sequence of instructions until a specific condition is met.
 Flow:
1. Perform an action.
2. Check a condition.
3. Repeat if condition not met; exit if met.
 Common purposes: Data retrieval, data manipulation, repetitive tasks.
 Types of loops:
1. While loop – Pre-test; executes body only if condition is true at the start.
2. For loop – Initializes counter once; runs until condition is false; updates counter
each iteration.
3. Do-while loop – Post-test; executes body at least once before checking condition
(exit-controlled).

5. Key Takeaways

 Branching = Choosing between actions.


 Looping = Repeating actions a set number of times or until a condition changes.
 Boolean logic (true/false decisions) and variables (changeable data) are core to both.
 Each construct (if, for, while, switch, etc.) serves a specific use case in controlling
program flow.
Topic 7: Introduction to Programming Concepts, Part 1
Introduction to Programming Concepts – Part 1

Learning Objectives

 Describe the purpose and use of identifiers in programming.


 Describe the purpose and use of containers in programming.

1. Identifiers

 Definition:
An identifier is a custom-named label assigned to a program component (e.g., stored
value, method, interface, class).
Used to reference program elements.

Types of Identifiers

1. Constants
o A data item whose value does not change during program execution.
o Examples:
 Numerical constant: π (Pi)
 Text constant: Player’s name in a game
o Also known as named constants.
o Assigned value at definition.
o Benefits:
 Improves code readability.
 Easy to update—change value only once in the constant definition.
2. Variables
o A data item whose value can change during execution.
o Examples:
 User’s age input
 High score in a game
o Can store:
 Strings
 Numbers
 Other data types
o Use case: Reference unknown or changing program items (e.g., username, file
name).
o Best practice: Use variables instead of hard-coding values.
o Declaration:
 Can define data type and initial value together.
 Or declare without initial value and assign later.
2. Containers

 Special identifiers that store multiple elements in a single structure.


 Avoids creating individual variables for each element.
 Efficiency: Better for large data storage (e.g., storing 1000+ integers).

Types of Containers

1. Arrays
o Store a fixed number of elements of the same type.
o Stored in sequential order starting from index 0.
o Declaration syntax:

dataType arrayName[maxSize];

o Size is fixed at creation.


2. Vectors
o Dynamic size: Automatically resize when elements are added or removed.
o Also called dynamic arrays.
o Use more memory than arrays.
o Access is slower than arrays because elements aren’t stored sequentially in
memory.
o Declaration syntax:

vector<dataType> vectorName;

o No need to specify max size.

Key Takeaways

 Identifiers reference program components; can be constants or variables.


 Constants: Fixed value, improve readability and maintainability.
 Variables: Changeable values, flexible for dynamic data.
 Arrays: Fixed-size, same-type elements, sequential storage.
 Vectors: Dynamic-size containers, more memory use, slower access.
Topic 8: Intro to Programming Concepts Part 2

🎯 Learning Objectives

 Understand the purpose and use of functions in programming.


 Understand the purpose and use of objects in programming.

🔹 Functions in Programming

 Definition:
A function is a structured, stand-alone, reusable block of code that performs one specific
action.
 Purpose:
o Breaks down a large, complex program into smaller, manageable parts.
o Improves modularity, reusability, and readability.
 Terminology:
o Different languages may call them: subroutines, procedures, methods, or
modules.
o Most modern languages use the term function.
 How Functions Work:
o Input: Functions take in data (parameters).
o Processing: Function performs some operations.
o Output: Function returns a result.
 Types of Functions:

1. Standard Library Functions (built-in, provided by the language).


 Examples: if, else, while, print.
2. User-defined Functions (written by the programmer).
 Can be reused many times once written.
 Defining Functions:

o Typically involves:
 A function keyword.
 A unique function name.
 The body of code/statements that define the function.
o Different programming languages use different syntax to define functions:
 Braces {} (C, Java).
 Begin/End statements (Pascal).
 Indentation (Python).
 Calling Functions:
o After definition, a function must be called (invoked).
o Calling runs the function’s code with any specified parameters.
 Function Declaration (in some languages like C/C++):
o A function must be declared before it is defined/used.

🔹 Objects in Programming

 Definition:
o Objects are the foundation of Object-Oriented Programming (OOP).
o They contain:
 Data → properties/attributes.
 Code → procedures/methods.
 Difference from Procedural Programming:
o Procedural: Methods operate on separate data structures.
o OOP: Combines data and methods inside the same object.
 Real-World Analogy:
o Think of objects like Car, Bike, TV, Washing Machine.
o Ask:
 What states can it be in? (e.g., Car → parked, moving).
 What behaviors can it perform? (e.g., Car → accelerate, brake).
o Complex objects → more states & more behaviors.
 Software Objects:
o Similar to real-world objects.
o Examples: Windows service, user account, database table, system folder.
 Structure of Software Objects:
o Properties (states) → stored in fields/variables.
o Methods (behaviors) → exposed through functions/methods.

✅ Key Takeaways

 A function is a stand-alone, reusable block of code that performs a specific action.


 Defining and calling functions is a universal concept across all programming languages.
 Object-Oriented Programming (OOP) focuses on objects (data + methods) rather than
functions alone.
 Software objects consist of:
o Properties (fields/variables).
o Methods (functions).
Summary & Highlights

Congratulations! You have completed this module. At this point, you know:

 Interpreted programming languages create source code that runs through an interpreter
and is built into your operating system (OS) on your computer or on your web browser.
 Compiled programming languages create executable files that are grouped in programs
on your computer or device.
 Query languages, structured programming languages, and object-oriented programming
languages are categorized as high-level programming languages and assembly
languages are categorized as low-level programming languages.
 The two main methods of organizing and planning code are by developing flowcharts
and by writing pseudocode. Flowcharts are pictorial representations of algorithms and
pseudocode is an explanation of the function of each line of a program.
 To reference a program component, software developers use an identifier, which can
either be a constant or a variable.
 A function is a piece of structured, stand-alone, and reusable code that will perform a
single specific action.
 Object-oriented programming is a programming paradigm based on the concept of
objects, which contain data and behavior through attributes and methods

You might also like