CSC 2301_201 Computer Programming I
CSC 2301_201 Computer Programming I
Computer programming is the art and science of designing, writing, testing, and
maintaining source code for computer systems. It involves instructing a computer
to perform specific tasks by providing it with a set of instructions in a
programming language. These instructions are then translated into machine code
that the computer can execute. Programming is the backbone of software
development, enabling the creation of applications, websites, games, and various
other digital solutions.
Components of Computer Programming
i. Variables and Data Types:
Variables: These are placeholders for storing data values. For example, in
a simple program, a variable named ‘age’ could store an individual's age.
Data Types: These define the type of data a variable can hold, such as
integers, floating-point numbers, strings, etc.
ii. Control Structures:
Conditional Statements: Allow the programme to make decisions based on
certain conditions, like if-else statements.
Loops: Enable the repetition of a set of instructions until a specific
condition is met.
iii. Functions and Procedures:
Functions: Segments of code designed to perform a specific task, often
reusable throughout a programme.
Procedures: Similar to functions but do not return a value.
iv. Arrays and Collections:
Arrays: Collections of variables of the same type, accessed using an index.
Collections: More dynamic structures that can store multiple elements of
different types.
Importance of Programming in Problem-Solving
Programming is a powerful tool for problem-solving in various domains. It allows
individuals to break down complex problems into manageable tasks and develop
step-by-step solutions. The key benefits of programming in problem-solving
include:
i. Algorithmic Thinking: Programming encourages the development of
algorithms, step-by-step procedures for solving problems. This systematic
approach is crucial in problem-solving.
ii. Automation and Efficiency: Through programming, repetitive and time-
consuming tasks can be automated, increasing efficiency and reducing the
likelihood of errors.
iii. Innovation and Creativity: Programming empowers individuals to create
new solutions, whether it's developing innovative software applications,
optimising business processes, or inventing new technologies.
iv. Critical Thinking: Programmers must think critically to identify and fix
errors in their code. This analytical mindset extends to problem-solving in
various aspects of life.
v. Interdisciplinary Applications: Programming is not limited to computer
science; it is used in diverse fields such as finance, biology, physics, and
more.
Brief History and Evolution of Programming Languages
The evolution of programming languages has been a fascinating journey, driven
by the need for more efficient and user-friendly ways to communicate with
computers.
Historical Milestones:
i. Early Programming Languages
Machine Code: Early computers were programmed using this lowest-level
programming language, consisting of binary code that directly
corresponded to the machine's architecture. Each instruction was
represented by a series of 0s and 1s.
Assembly Language: To make programming more manageable, assembly
languages were developed. They used mnemonics and symbols to
represent machine code instructions, making it easier for programmers to
write and understand code.
ii. High-Level Programming Languages
Fortran (1957): Developed by IBM, Fortran was initially created for
scientific and engineering calculations. It introduced high-level constructs
like loops, conditional statements and subroutines, making it easier for
programmers to express complex mathematical computations.
COBOL (1959): COBOL was designed for business applications,
emphasising readability and ease of use. It played a significant role in data
processing and business-oriented software development.
C (1972): Created by Dennis Ritchie at Bell Labs, C became a powerful
and efficient language for system programming. It provided low-level
access to memory and hardware, making it suitable for operating systems
and embedded systems. C, with its simple syntax and powerful features,
became a foundational language for systems programming.
iii. Object-Oriented Languages
C++ (1983): Bjarne Stroustrup developed C++ as an extension of C,
introducing object-oriented programming concepts. It allowed for the
creation of classes and objects, facilitating code organisation and reuse.
Java (1995): Java aimed to solve the “write once, run anywhere” problem.
It introduced the concept of platform-independent bytecode, making it
possible to execute Java programmes on any device with a Java Virtual
Machine (JVM).
Python (1991): Guido van Rossum designed Python with a focus on
readability and simplicity. Its versatile nature and support for multiple
programming paradigms, including procedural and object-oriented,
contributed to its popularity. Python has become popular in web
development, data science, and artificial intelligence.
iv. Modern Trends
Rust (2010): Developed by Mozilla, Rust focuses on system programming
with an emphasis on memory safety, zero-cost abstractions, and
concurrency without data races. It aims to provide the performance of low-
level languages while preventing common programming errors.
Kotlin (2011): JetBrains developed Kotlin as a modern, expressive
language that runs on the Java Virtual Machine (JVM). It is interoperable
with Java, making it an excellent choice for Android app development.
Swift (2014): Apple introduced Swift as a replacement for Objective-C in
iOS and macOS development. It is designed to be more concise, safe, and
modern, with features that enhance developer productivity.
Programming Languages
Programming languages serve as crucial intermediaries between humans and
computers, facilitating the translation of human-readable instructions into
machine-executable code. Programming languages have evolved through various
stages. These include machine language, assembly language, and high-level
language.
Machine Language
Machine language is the lowest-level programming language, consisting of
binary code that directly communicates with a computer's central processing unit
(CPU). Each instruction is represented by a unique combination of 0s and 1s.
Example: Machine Language Programme
The 8086 Machine language programme for var1 = var2 + var3 is as follows:
1010 0001 0000 0000 0000 0000 0000
0011 0000 0110 0000 0000 0000 0010
1010 0011 0000 0000 0000 0000
Characteristics:
i. Binary Representation: Machine language instructions are represented in
binary, reflecting the low-level operations understood by the computer's
hardware.
ii. Processor Specific: Programmes written in machine language are tied to
the architecture of a specific processor, making them non-portable between
different types of CPUs.
iii. Difficult for Humans: Writing programmes in machine language is
extremely challenging for humans due to its complex and error-prone
nature.
iv. Direct Execution: Machine code is executed directly by the CPU, making
it the most efficient in terms of execution speed.
Assembly Language
Assembly language is a low-level programming language that provides a
symbolic representation of machine code. It uses mnemonics and symbols to
represent machine instructions, making it more readable than machine language.
Characteristics:
i. Symbolic Representation: Assembly language uses symbols and
mnemonics to represent machine instructions, making it easier for
programmers to read and write.
ii. One-to-One Correspondence: Each assembly language instruction
typically corresponds to a single machine language instruction.
iii. Processor-Specific: Like machine language, assembly language is
processor-specific, as different processors have different instruction sets.
iv. Direct Interaction with Hardware: Assembly language allows direct
interaction with hardware components, making it suitable for tasks that
require precise control over hardware resources.
High-Level Language
High-level programming languages are designed to be closer to human language,
making them more readable and easier to write. They are independent of the
underlying hardware and provide a level of abstraction from machine-specific
details.
Characteristics:
i. Abstraction: High-level languages abstract away the low-level details of
the hardware, allowing programmers to focus on the logic of their
programmes.
ii. Portability: Programmes written in high-level languages are generally
portable across different platforms, as they are not tied to the specifics of
the underlying hardware.
iii. Readability and Writability: High-level languages are designed to be
readable and writable, making it easier for programmers to understand and
express complex ideas in a more natural syntax.
iv. Automatic Memory Management: Many high-level languages include
features for automatic memory management, reducing the risk of memory-
related errors.
Examples of High-Level Languages:
i. Python: known for its simplicity and readability, Python is widely used in
web development, data science, and artificial intelligence.
ii. Java: Java's platform independence and object-oriented features make it
suitable for building a wide range of applications.
iii. C++: an extension of C, C++ adds object-oriented features, making it
powerful for system-level programming and game development.
iv. JavaScript: primarily used for web development, JavaScript is a high-
level language that runs in web browsers.
Programming Paradigm
Programming paradigms define the approach and style of computer
programming. Each paradigm provides a set of principles and practices for
designing and structuring code. Understanding different programming paradigms
allows programmers to choose the most suitable approach for a given problem.
Common Programming Paradigms
i. Imperative Paradigm: The imperative paradigm focuses on describing
the steps that a programme should take to reach a particular state. It is based
on the concept of changing programme state through a sequence of
statements. Imperative programming uses control structures like loops and
conditional statements to guide the flow of execution. Example of
imperative languages: C, FORTRAN, Pascal, and COBOL.
ii. Object-Oriented Paradigm: Object-oriented programming (OOP)
organises code around objects, which are instances of classes. Objects
encapsulate data and behavior, promoting modularity and reusability. OOP
introduces the concept of classes, which define the blueprint for objects.
Objects are instances of these classes. Key features include encapsulation,
inheritance, and polymorphism. Encapsulation involves bundling data and
methods that operate on that data within a single unit (object). This helps
in hiding the internal details of an object. Inheritance allows one class to
inherit properties and methods from another, facilitating code reuse and
creating a hierarchy of classes. Polymorphism enables objects of different
types to be treated as objects of a common type, simplifying code and
making it more adaptable. Example of object-oriented languages: Java, C#,
and Smalltalk.
iii. Functional Paradigm: The functional paradigm treats computation as the
evaluation of mathematical functions. It avoids changing state and mutable
data, relying on the concept of immutability and pure functions. Functions
in the functional paradigm are pure, meaning they produce the same output
for the same input and do not have side effects. Functions are treated as
first-class citizens, meaning they can be assigned to variables, passed as
arguments, and returned as values. Higher-order functions take other
functions as parameters. Immutability is another key feature of functional
paradigm that prevents the modification of data once it's created. Instead
of changing existing data, new data structures are created. Example of
functional languages: ML, Miranda TM, Scala, Clojure and Haskell.
iv. Logic Programming: Logic programming is based on formal logic.
Programmes in this paradigm express relationships and rules as logical
statements, and the execution involves finding solutions to logical queries.
Logic programming is characterised by distinctive features. Firstly, it
adopts a declarative style, diverging from the imperative paradigm, where
programmers articulate what needs to be achieved rather than specifying
how to achieve it. Secondly, the structure of logic programmes is built upon
rules and facts. Rules define relationships and conditions, shaping the
programme's logic, while facts provide the base knowledge. Lastly, logic
programming employs backtracking as a fundamental mechanism. This
enables the exploration of alternative solutions when searching for a
satisfying answer to a query, contributing to the language's flexibility and
problem-solving capabilities. Example of logic programming language:
Prolog.
Scripting Languages
Scripting languages are programming languages that are often interpreted and
designed for executing scripts, which are sets of commands written for a specific
runtime environment. These languages prioritise ease of use and quick
development, making them suitable for various applications. Examples: Python,
JavaScript, and Shell Scripting.
Python stands out as a high-level, general-purpose scripting language renowned
for its readability and versatility. Its syntax emphasises code readability, making
it an accessible language for both beginners and experienced developers. Python
supports multiple programming paradigms, including procedural, object-
oriented, and functional programming, providing flexibility in coding styles. One
of Python's strengths lies in its extensive standard library, offering pre-built
modules and packages that simplify the development process. Python is key in
web development (Django, Flask), data analysis (NumPy, Pandas), machine
learning (TensorFlow), automation, and network programming.
JavaScript is a versatile scripting language primarily utilised for web
development, enabling the creation of dynamic and interactive content in web
browsers. As a client-side language, JavaScript enhances user experience by
enabling functionalities such as form validation and asynchronous data retrieval.
Moreover, its versatility extends to server-side development through platforms
like Node.js, expanding its role beyond the browser. JavaScript is a cornerstone
in front-end web development, powering popular frameworks like React,
Angular, and Vue.js. It also finds application in server-side development using
Node.js, where its event-driven architecture contributes to efficient handling of
concurrent connections. Beyond web development, JavaScript is employed in
game development through frameworks like Phaser, showcasing its adaptability
across various domains.
Shell scripting involves the use of a command-line shell to execute a series of
commands in a script. It is a powerful tool for automating system tasks and
managing processes in a Unix-like environment, offering efficient and concise
solutions for various tasks. Shell scripting is indispensable in system
administration and configuration tasks, automating repetitive processes and
ensuring consistency across systems. It excels in file and data manipulation,
providing quick and efficient ways to process and analyze data. Additionally,
shell scripts are widely employed for tasks such as automated backups and
scheduled jobs (cron jobs), making them a vital component of system
maintenance and management.
Important Features of Scripting Languages
i. Interpreted Execution: Scripting languages are often interpreted rather
than compiled, allowing for immediate execution of code without the need
for a separate compilation step.
ii. Dynamic Typing: Scripting languages typically use dynamic typing,
where variable types are determined at runtime. This flexibility simplifies
code writing but may lead to runtime errors.
iii. High-Level Abstraction: Scripting languages provide high-level
abstractions, making it easier for programmers to express concepts without
dealing with low-level details.
iv. Rapid Development: The focus on ease of use and quick development
makes scripting languages ideal for tasks like automation, web
development, and system administration.
Figure 1: JIT Compile Process (Java Programming Environment and the Java Runtime
Environment, n.d.)
Java Programmes Structure
Source Programme
Components
i. Class Definition: A Java programme typically consists of one or more
classes. Each class is defined using the ‘class’ keyword.
ii. Main Method: The ‘main’ method serves as the entry point of a Java
program. It is where the programme starts executing.
iii. Statements: Java programmes are composed of statements that perform
actions. Statements end with a semicolon (;).
Syntax:
public class ClassName {
public static void main (String [] args) {
statement 1
statement 2
…
Statement N
}
}
Example:
public class HelloWorld {
public static void main (String[] args) {
System.out.println(“Hello, World!”);
}
}
The above example should be typed and saved as " HelloWorld.java." It's crucial
to bear in mind that Java is case-sensitive and follows a free-form layout. Specific
words such as public, class, static, void, main, and others are designated as
reserved or keywords, and their meanings are fixed by the language. The first
line, "public class HelloWorld," signifies the creation of a new class named
Main Method
The ‘main’ method serves as the entry point of a Java programme and is declared
as follows:
public static void main(String[] args) {
// Main method body
}
if (condition) {
// Code block executed if the condition is true
} else {
// Code block executed if the condition is false
} Example:
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is not greater than 5");
}
while (condition) {
// Code block executed as long as the condition is true
}
Example:
int count = 0;