You are on page 1of 31

CHAPTER 1: INTRODUCTION

TO PROGRAMMING
CSEB2113 PROGRAMMING I

Badariah Solemon

BS SEPT 2021 Photo by AltumCode on Unsplash


MODULES

1.1 Problem Solving, Program and


Programming
1.1.1 Computer Program and Programming
1.1.2 Problem Solving and Programming

1.2 Programming Languages


1.2.1 Types of Programming Languages
1.2.2 The C Language

BS SEPT 2021
PROBLEM SOLVING, PROGRAM AND
PROGRAMMING
Section 1.1

BS SEPT 2021
1.1.1 COMPUTER PROGRAM AND PROGRAMMING

BS SEPT 2021
COMPUTER AND PROGRAM
• Computers:

Wearable

Personal Computer Handheld

Desktop

Mainframe

Tablet Workstation Server


Laptop Supercomputer

• Computers can do a lot of exciting TASKS.


BS SEPT 2021
COMPUTER AND PROGRAM
• What computers can do?

INPUT COMPUTER OUTPUT

Off False True On


0 1

• Must be GIVEN INSTRUCTIONS before they can perform actions and make
decisions.
• A set of instructions = Computer Program/Software/Code .
BS SEPT 2021
COMPUTER PROGRAMS
• Can be embedded into hardware to control its operations – embedded
system.

Satellites

Smart Vehicles

Consumer electronics
Medical Equipment Smart Cards
BS SEPT 2021
WHAT IS PROGRAMMING?
• Often called CODING or SCRIPTING
• Not only about writing computer programs

The ITERATIVE process of designing, writing, testing, debugging, and


maintaining the SOURCE CODE of computer programs

• Source code is written in PROGRAMMING LANGUAGES


BS SEPT 2021
SAMPLE SOURCE CODE

BS SEPT 2021
WHY DO CODING?

https://www.youtube.com/watch?v=Dv7gLpW91DM
BS SEPT 2021
1.1.2 PROBLEM SOLVING AND PROGRAMMING

BS SEPT 2021
PROBLEM SOLVING IN EVERYDAY LIFE
• People make decisions every day to solve problems that affect their lives.
• Problems
• Unimportant - what to watch on TV
• Important - choosing a degree program

If a bad decision is made, time and resources are wasted, so it’s important
that people know how to make decisions well.

BS SEPT 2021
COMMON STEPS TO SOLVE PROBLEMS

6
5
4 EVALUATE the
3 SOLVE the solution
2 Select the BEST problem
1 Identify
WAY to solve
problem
ALTERNATIVES
UNDERSTAND the
problem
IDENTIFY the
problem

BS SEPT 2021
PROGRAMMING & PROBLEM SOLVING
• Programming is a problem solving activity.
• When you write a program, you are actually writing INSTRUCTIONS for the computer
to SOLVE something for you.

BS SEPT 2021
PROGRAMMING LANGUAGES
Section 1.2

BS SEPT 2021 Photo by Soner Eker on Unsplash


WHAT IS A PROGRAMMING LANGUAGE?
• Technically, it is an artificial language
• designed to communicate instructions to, or to control the behaviour of a machine,
particularly a computer.
• It is different from our everyday-language
• Natural Language:
• Depends on circumstances; the context – one word can have many meaning depending on
the situation (e.g., OPERATE)
• Programming Language:
• Very specific (one word means one thing – context free) since to 'talk' to a computer; to
instruct a computer; our commands must be 100% clear and correct.

BS SEPT 2021
WHAT IS A PROGRAMMING LANGUAGE?
• Conceptually, programming language is a FRAMEWORK within which we
organize our ideas about data and procedures.

1. DATA 2. PROCEDURES
'objects’ / ‘things’ 'rules’ / ‘processes’
we want to define how to manipulate
manipulate data

• Try this out:


• For a program to determine sum of two numbers?

BS SEPT 2021
1.2.1 TYPES OF PROGRAMMING LANGUAGES

BS SEPT 2021 Photo by Michael Dziedzic on Unsplash


TYPES OF PROGRAMMING LANGUAGES
printf (“Hello”);
total = quiz + assignment;
H ig
h-le
vel g u age printf(“Total = %d”, total);
L an
LOAD A, 9999
Ass LOAD B, 8282
emb g u age SUB B
ly L an MOV C, A

Mac
h in e g u age 1101 0001 1000 0101 1000 0110
L an 0100 1100 0101 1110 0101 1010

BS SEPT 2021
WHAT IS A MACHINE LANGUAGE?
• The only language that is directly understandable by a computer’s processor.
• Certainly difficult for humans to understand.
• Consists of binary codes: 0 and 1 .

1101 0001 1000 0101 1000 0110 1111 0001


0100 1100 0101 1110 0101 1010 0001 1100

BS SEPT 2021
PROGRAMMING IN MACHINE LANGUAGE
• The language into which all programs MUST BE CONVERTED before they can
be run.
• Generally MACHINE DEPENDENT , as it varies from processor to processor
(e.g., Intel, AMD and ARM processors).
• NOT PORTABLE
• Programs may sometimes have to be completely rewritten to work on different type
of processors.
• Programming in machine language is VERY SLOW AND TEDIOUS since it is
hard to memorize all the instructions and mistakes can happen very easily.
BS SEPT 2021
WHAT IS AN ASSEMBLY LANGUAGE?
• Uses a MNEMONIC CODE to represent each machine operation.
• in words and numbers
• An intermediary language that can be UNDERSTOOD by humans.
• but is still quite difficult to use
//j = 25;
mov r3, #25
str r3, [r11, #-12]
• Processor dependent and not portable.
BS SEPT 2021
PROGRAMMING IN ASSEMBLY LANGUAGE
• Cannot be processed directly by a computer, must be converted to machine
language using ASSEMBLER.
• Easier to use than ML, BUT programmer must completely understand the
computer hardware in order to program it.

BS SEPT 2021
WHAT IS A HIGH-LEVEL LANGUAGE?
• A programming language designed to simplify computer programming
• Written in a form that is close to our human language, enabling to programmer to
just focus on the problem being solved.
• PORTABLE
• Easier to write, modify and debug as it uses English like statements
• Example:

BS SEPT 2021
PROGRAMMING IN HIGH-LEVEL LANGUAGE
• The programming structure is PROBLEM ORIENTED
• does not need to know how the computer actually executes the instructions.
• Processor independent
• the same code can be run on different processors.
• Programs written in these languages must be translated into executable
machine language using:
• Compilers
• Linkers

BS SEPT 2021
PROGRAMMING IN HIGH-LEVEL LANGUAGE

‘MAGIX BOX’

PROGRAM COMPILER MACHINE LANGUAGE

LINKER

Compilers – translate instructions to create object files


Linkers – combine 1 or more object files along with library file to make
EXECUTABLE file

BS SEPT 2021
1.2.2 THE C LANGUAGE

BS SEPT 2021 Photo by Dawid Zawiła on Unsplash


FACTS AND APPLICATIONS OF C
• It was developed by Dennis Ritchie in the early 1970s at Bell Laboratories
(now a part of Lucent Technologies, Inc).
• A high-level language that is HIGHLY PORTABLE.
• A powerful general-purpose programming language.
• Can be used to develop software like operating systems, databases, compilers, and
so on.
• It has influenced the newly created high-level language name Objective-C.
• The main programming language used by Apple for the OS X and iOS operating
systems and their respective APIs, Cocoa and Cocoa Touch frameworks.

BS SEPT 2021
YOUR FIRST C PROGRAM D+R+E

• C SOURCE CODE

1. Translate into machine language.


2. Execute/run the program to produce OUTPUT.
BS SEPT 2021
RECAP

?
? ?
PROBLEM / IDEA PROGRAM EXECUTABLE FILE SOLUTION
High-level Language Machine Language

BS SEPT 2021
BS SEPT 2021

You might also like