0% found this document useful (0 votes)
13 views25 pages

Flowchart and Pseudocode(2)(2)(2)

Uploaded by

pranav.garg1006
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)
13 views25 pages

Flowchart and Pseudocode(2)(2)(2)

Uploaded by

pranav.garg1006
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/ 25

Basic Program Structure

Part A: Flowchart and Pseudo-code

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 2


Lesson Objectives

At the end of this lesson, you should be able to:

• Define what an algorithm is

• Express an algorithm using


• Flowcharts
• Pseudo-code

• Formulate a simple problem

• Express the solution(s) of a problem in such a way that a


computer—human or machine—can effectively carry out

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 3


Topic Outline

Algorithms

Flowcharts

Pseudocode

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 4


Scenario 1: Finding the Nearer Coffee Shop

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 5


Algorithms

Algorithms are basically sequential (step-by-step).

Sequence: Step 1 Step 2 Step 3 …

Scenario 1: Finding the Nearer Coffee Shop

Calculate the distances to locations

Find the nearer location

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 6


Algorithms (Cont’d)

• When you formulate a method/ procedure for solving a problem, it has to be computable.
• Such a procedure is called Algorithm.

Scenario 1: Finding the Nearer Coffee Shop

Find the distance to location 1


3
Calculate the distances to locations
Find the distance to location 2

5
Compare the two distances
Find the nearer location
Select the nearer one based on comparison result

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 7


Algorithms (Cont’d)

How to construct an Algorithm?

General Notes:
• No strict rules

• Uses informal language – combination of English and keywords

Common Keywords Other Keywords


IF, ELSE, WHILE READ, PRINT, INITIALIZE,
COMPUTE, ADD, SUBTRACT

• Usually starts an operation sentence with a verb (description should be concise and precise)

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 8


Flowcharts

Flowchart: a representation of an algorithm using diagram for effective visualization

START Name Symbol Use in Flowchart


INPUT Oval Denotes the beginning or end of a program

PROCESS Flow line Denotes the direction of logic flow in a program

Denotes either an input operation (e.g., INPUT or


DECISION Parallelogram
an output operation (e.g. PRINT)
Rectangle Denotes a process to be carried our (e.g. an
OUTPUT addition)
Denotes a decision or branch to be made; the
END Diamond
program should continue along one of two routes

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 9


Flowcharts (Cont’d)

Scenario 1: Flowchart 1
Start

Find the distance to location 1


IF (A = TRUE)
Find the distance to location 2 Then B
Else C
May include Branching End IF
Compare the two distances (making selection) True False
A

Select the nearer one based on


the comparison result B C

End

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 10


Flowcharts (Cont’d)

Scenario 1: Flowchart 1 Flowchart 2


Start Start

Find the distance to location 1 Find the distance to location 1

Find the distance to location 2 Find the distance to location 2


Symbolize
Compare the two distances

NO
Select Is distance 1 Select
Select the nearer one based on Location 1 <= distance 2? Location 2

YES
the comparison result

End End

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 11


Flowcharts (Cont’d)

What if there were many coffee shops?

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 12


Flowcharts (Cont’d)

What if there were many coffee shops?


Entry point

Start

Find the distance to location 1 False


May include ?
Looping
Find the distance to location 2 (repeating True
certain
Find the distance to location 3 operations)

Find the distance to location 4

Exit point

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 13


Flowcharts (Cont’d)

May include Start


Looping
(repeating certain n=1
operations)
No
n<=8
Yes
Find the distance to location n

n increases by 1

Data Structure (Ch. 4); Function (Ch. 5 & 6);


Find nearest location
Algorithm (Ch.7)
More on this later..

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 14


Pseudocode

Pseudocode: pronounced as /ˈs(j)uːdəʊˌkəʊd/

• IDEA: directly uses informal English to describe an algorithm step by


step with one step per line

• Uses the structural conventions of a normal programming language


- but is intended for human reading rather than machine reading

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 15


Pseudocode (Cont’d)

1
Guidelines
2
3 • Write one statement per line only
• Capitalize the keywords
• Indent to show hierarchy
• End multi-line structures
• Keep statements programming-language independent

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 16


Flowchart vs Pseudocode

Flowchart

Start Pseudocode
Find the distance to location 1 FIND the distance to location 1
FIND the distance to location 2
Find the distance to location 2
IF distance1 < = distance2
Symbolize
SELECT Location 1
Select Is distance1 NO Select
Location 1 <= distance2? Location 2 ELSE
YES

SELECT Location 2

End END IF

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 17


Summary: Expressing an Algorithm

Must be
unambiguous

Consider all
Every step must be Specify the order
possible decision points
clear and precise of steps precisely
[Branching and
[Sequence]
Looping]

Must terminate
(No matter which representation is used)

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 18


Scenario 2

Making Scrambled Eggs

1. Beat the eggs for 20 to 35 seconds in a bowl

2. Heat a frying pan over a medium-low heat

3. Melt some butter in the frying pan

4. Cook eggs on the pan and stir eggs while


cooking

5. Add other ingredients

6. Serve the scrambled eggs

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 19


Scenario 2 (Making Scrambled Eggs): Flowchart

Start Beat the eggs in a bowl

Heat the frying pan


Can repeat
Melt butter and put the eggs on the pan

N
Are eggs ok? Stir the eggs
Decision blocks
Y
(make choices)
Any other Y
Add other ingredients
ingredients?
Output! N
Serve Scrambled Eggs
Can repeat
End

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 20


Scenario 2 (Making Scrambled Eggs): Pseudocode

BEAT the eggs for 20 to 35 seconds in a bowl


HEAT a frying pan over a medium-low heat
MELT some butter in the frying pan and PUT eggs on pan
WHILE eggs not okay
STIR eggs while cooking
END WHILE
IF any ingredients
Add other ingredients
END IF
SERVE the scrambled eggs

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 21


Quick Check

Is an algorithm readable by computers?

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 22


Quick Check: Answer

Is an algorithm readable by computers?

Answer No

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 23


References for Images

Placeholder

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 24


Knowledge Check Questions

CX1003 Introduction to Computational Thinking: Basic Program Structure Part A 25

You might also like