0% found this document useful (0 votes)
8 views36 pages

BSCS1350-3

The document is an introduction to programming, covering topics such as how programs work, the difference between machine and assembly languages, and the use of high-level languages. It discusses the importance of designing programs, including the creation of algorithms, pseudocode, and flowcharts. Additionally, it explains the roles of compilers and interpreters in translating code for execution.

Uploaded by

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

BSCS1350-3

The document is an introduction to programming, covering topics such as how programs work, the difference between machine and assembly languages, and the use of high-level languages. It discusses the importance of designing programs, including the creation of algorithms, pseudocode, and flowcharts. Additionally, it explains the roles of compilers and interpreters in translating code for execution.

Uploaded by

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

Introduction to Programming

Introduction to Programming - Algorithms


(BSCS 1350)
Dr. Ghadah Alghamdi
[email protected]

1
Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Introduction
• Computers can be programmed:
• Designed to do any job that a program tells them to

• Program: set of instructions that a computer follows to perform a task


• Commonly referred to as Software

• Programmer: person who can design, create, and test computer


programs
• Also known as software developer
Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
How a Program Works
• CPU designed to perform simple operations on pieces of data
• Examples: reading data, adding, subtracting, multiplying, and dividing
numbers
• CPU Interprets and executes commands written in machine language
as defined by its instruction set
• Each CPU brand has a unique instruction set, defining the specific
commands it can execute.

5
How a Program Works
• Program must be copied from secondary memory to RAM each
time CPU executes it

• CPU executes program in cycle:


• Fetch: read the next instruction from memory into CPU
• Decode: CPU decodes fetched instruction to determine which
operation to perform
• Execute: perform the operation
How a Program Works

Figure 1-16 The fetch-decode-execute cycle


Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
From Machine Language to Assembly Language

• Impractical for people to write in machine language

• Assembly language: uses short words for instructions instead of


binary numbers
• Easier for programmers to work with

• Assembler: translates assembly language to machine language for


execution by CPU
High-Level Languages
•Low-level language: close in nature to machine language
• Example: assembly language

•High-Level language: allows simple creation of powerful and


complex programs
• No need to know how CPU works or write large number of
instructions
• More intuitive to understand
Key Words, Operators, and Syntax
• Key words: predefined words used to write program in high-level
language
• Each key word has specific meaning

• Operators: perform operations on data


• Example: math operators to perform arithmetic

• Syntax: set of rules to be followed when writing program

• Statement: individual instruction used in high-level language


Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Compilers and Interpreters
•Programs written in high-level languages must be translated
into machine language to be executed

•Compiler: translates high-level language program into


separate machine language program
• Machine language program can be executed at any time
Compilers and Interpreters
•Interpreter: translates and executes instructions in high-level
language program
• Used by Python language
• Interprets one instruction at a time
• More efficient but slower than compiler

•Source code: statements written by programmer


• Syntax error: prevents code from being translated
Compilers and Interpreters

Figure 1-19 Executing a high-level program with an interpreter


Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Designing a Program
• Programs must be designed before they are written

• Program development cycle:


• Design the program
• Write the code
• Correct syntax errors
• Test the program
• Correct logic errors
Designing a Program
• Design is the most important part of the program
development cycle
• Understand the task that the program is to perform
• Work with customer to get a sense what the program is
supposed to do
• Ask questions about program details
• Create one or more software requirements
Designing a Program
• Determine the steps that must be taken to perform the task
• Break down required task into a series of steps
• Create an algorithm, listing logical steps that must be
taken

• Algorithm: set of well-defined logical steps that must be


taken to perform a task
Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Pseudocode
• Pseudocode: fake code
• Informal language that has no syntax rule
• Not meant to be compiled or executed
• Used to create program model (design)
• No need to worry about syntax errors, can focus on
program’s design
• Can be translated directly into actual code in any
programming language
Pseudocode - Example
• An algorithm to check if the student passed or failed:

Declare variable grade


Print “please enter your final grade”
Read grade
If grade is greater than or equal to 60
Print "passed"
else
Print "failed"
Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Flowcharts
• Flowchart: diagram that graphically depicts the steps in a
program
• Ovals are terminal symbols (Start or End point)
• Parallelograms are input and output (I/O) symbols
• Rectangles are processing symbols
• Diamonds are decision symbols
• Symbols are connected by arrows that represent the flow
of the program
Flowcharts
Initialize Hours = 0,
payrate = 0, grossPay = 0 Ovals are
terminal symbols

Parallelograms are input


and output symbols

Rectangles are
processing symbols

Ovals are
terminal symbols
Problem Solving – Example 1
• Problem: Add two numbers entered by the user.

Step 1: write the algorithm (Pseudocode)


• Declare variables num1, num2, sum
(process)
• Enter (Read) num1 (I/O)
• Enter (Read) num2 (I/O)
• sum = num1 + num2 (process) sum = num1 + num2

• Display (Print) sum (I/O)


Step 2: draw flowchart
Problem Solving – Example 2 (a second approach)

• Problem: Add 10 and 20 Start

Step 1: write the algorithm (Pseudocode) sum= 0


n1 = 10
• Initialize sum = 0, n1=10, n2=20 (process) n2 = 20
• Add the two numbers and store in sum
(process)
• Display sum (I/O) sum = n1 + n2

Print sum

Step 2: draw flowchart


End
Problem Solving – Example 3
(The Pay-Calculating Program)
Start

• Problem: Calculate the gross pay of an employee Initilise Hours =0, payrate = 0,
grossPay = 0
Step 1: write the algorithm (Pseudocode) Print “Enter hours
• Declare variable grossPay, Hours, PayRate worked (H) and hourly
pay rate (P)”
(process)
• Print “Enter hours worked and hourly pay Read hours worked (H)
Read hourly pay rate (P)
rate”
• Read hours worked (I/O)
grossPay = H*P
• Read hourly pay rate (I/O)
• Multiply hours worked by hourly pay rate and
Print grossPay
store in grossPay (process)
• Display grossPay (I/O)
End
Step 2: draw flowchart
Problem Solving – Example 4
• Problem: Check if the student passed or failed:

Step 1: write the algorithm (Pseudocode)


• Declare variable grade
• Print “please enter your final grade”
• Read grade
• Check grade:
▪ If grade is greater than or equal to 60
o Print "passed"
▪ Else
o Print "failed"
Problem Solving – Example 4
• Problem: Check if the student
Start

passed or failed Declare variable grade

Step 2: draw flowchart Print “please write your


grade”

Read grade

YES NO
Is grade >= 60?

Print “passed” Print “failed”

End
Flowcharts – Example 5 (Facebook Login)

• Problem: login to Facebook account


Step 1: write the algorithm (Pseudocode)
(1) Enter www.facebook.com in browser (I/O)
(2) Facebook page loads (process)
(3) Enter email and password (I/O)
(4) Check validity (decision)
▪ If false then
o Throw an error (process)
o Go to step 3
▪ Else
o Display Facebook account (I/O)
Flowcharts – Example 5 (Facebook Login)

Step 2: draw flowchart


Lecture Outline
• Introduction
• How a Program Works
• From Machine Language to Assembly Language
• High-Level Languages
• Key Words, Operators, and Syntax
• Compilers and Interpreters
• Designing a Program
• Pseudocode
• Flowcharts
• Class Exercise
Class Exercise
Write the pseudocode and draw the flowchart of the following
program:
• Multiply a number that is input by the user, by the number 5. Then
display the message “Greater than 100” if the multiplication result is
greater than 100. Otherwise, display the message “Less than or equal
to 100”
Thank you

36

You might also like