0% found this document useful (0 votes)
6 views

Representing Algorithm With Flowchart & Pseudocode

Uploaded by

tansuhbless
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Representing Algorithm With Flowchart & Pseudocode

Uploaded by

tansuhbless
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Representing

Algorithm with
Flowchart &
Pseudocode
Algorithms

 Algorithms can be represented using pseudocode


or a flowchart. It cannot be executed by the
computer.
 Pseudocode uses English–like statements that
models or resembles a programming language.
 A flowchart uses geometrical objects.
Algorithmic Structure
 Terminator: Start of statement(s)
 Declaration: Initialization of variables, if necessary
 Body: Sequence of steps
 Terminator: End of statement(s)
 E.g.
Examples of Algorithms and
Flowchart
 Design an algorithm and the corresponding
flowchart for adding the test scores as given: 26,
49, 98, 87, 62, 75
 Write the steps that would be used to add the test
scores
1. Start
STEPS
2. Sum = 0
3. Get the first testscore
4. Add first testscore to sum
5. Get the second testscore
6. Add to sum
7. Get the third testscore
8. Add to sum
9. Get the Fourth testscore
10. Add to sum
11. Get the fifth testscore
12. Add to sum
13. Get the sixth testscore
14. Add to sum
15. Output the sum
16. Stop
Flow chart
Control Structures

 In programming, control structures are used to


represent logic. There are different types of control
structures: sequential, selection and loop
(repetition). Just like a program, the body of an
algorithm is made up of various control structures.
Sequential Structures
Sequential structures include:
 Input statements: these accept data entered into the computer and store the
value in the location with the given variable name e.g.
input name
get num1, num2
read price and tax_rate
accept option
 Output statements: these display/output data that is in the computer’s
memory e.g.
Output Sum
Print total_cost
Display “ Enter student’s name”
Write sum, average
 Assignment statements: gives the value on the right of the assignment
operator (equal sign) to the variable on the left of the assignment operator e.g.
N= 100
Count = 0
Sequential Structures
 Calculation statements: uses the values on the right of the
assignment operator and performs mathematical operations, then
assigns the result of the calculation to the variable on the left of
assignment operator e.g.
Sum = num1 + num2
Difference = Payment – bill
Average = sum ÷ 3
 Prompting statements: these are used with input statements to
request or notify the user to enter data into the computer. These
statements are displayed on the screen. Prompting statements
precede input instructions. E.g.
Print “Enter student name: “ Prompting statement
Read name
Conditional Branching-IF THEN
 The IF-THEN construct contains a condition that is tested
before and action can be undertaken. They perform comparisons
between values and make a decision based on the result of the
comparison
Syntax:
IF <condition> THEN
<Action to be taken if conditions are true>
ENDIF
 For e.g.
IF ( A > B) THEN
Display A
ENDIF

Conditional Branching-IF THEN-ELSE
 The IF-THEN-ELSE construct contains two parts: the THEN part
and an ELSE part. The condition that is tested before and
action can be undertaken. If the condition is true, the THEN
action is taken. If the condition is false, the ELSE action is
taken.
 Syntax:
IF <condition> THEN
<THEN part: Action to be taken if condition is true>
ELSE
<THEN part: Action to be taken if condition is false>
ENDIF
 For e.g. IF( age >= 50) THEN
Print “Old”
 ELSE
Print
“Young”
Selection Structures

 If the decision consists of more than one instruction


statement. These statements must be enclosed in a BEGIN
and an END e.g.
IF (A > B) THEN
BEGIN
C=A+B
Print C
END
ENDIF
Loop Structures

 A group of statements/instructions that is repeated throughout


the program is called a loop.
There are two types of loops:
 A finite loop: where the number of time the instructions are
repeated are known.
 An infinite loop: where the instructions are repeated an
unspecified number of times until a result is achieved.
 Two methods used to repeat instructions are FOR construct
and WHILE construct.
FOR loop
 In the FOR construct, the loop id controlled by a counter that
increases each time the set of instructions are executed. This
construct is used when the number of times a set number of
instructions has to be repeated is known (finite loop).
 Syntax:
FOR <variable> = <Beginning value> TO <Ending value> DO
<action to be repeated>
ENDFOR
Or
FOR <variable> = <Beginning value> TO <Ending value> STEP
<incremental value> DO
<action to be repeated>
ENDFOR
 Example
FOR count = 1 to 5 DO
Print count
END FOR
FOR loop
Flowchart

 FOR count =
1 to 5
Print count
WHILE CONSTRUCT
 In the WHILE construct a set of statements/instructions are repeated
for as long as the condition is true. This WHILE construct should be
used when the number of times the instructions are to be repeated
are unknown.
 In the WHILE construct, the condition is tested if it is true, the
instructions are repeated until the condition becomes false.
 Instructions before the WHILE construct are carried out once. The
WHILE loops is repeated until the condition becomes false which
forces the loop to stop.
 If, after carrying out the instructions befpre the loop, the condition is
tested and is false, the instructions is skipped and the computer then
continues with the statements after the ENDWHILE.
 The terminating causes the loop to stop.
 Syntax:
WHILE <condition> DO
<action to be taken if condition is true>
WHILE loop Examples
WHILE (noOfItems <= 5) DO
BEGIN
Read price
noOfItems = noOfItems + 1
Total =Total + price
END
END WHILE
Or
cars = 0 counter variable is initialized to zero
WHILE cars <=10 DO
BEGIN
PRINT “ENTER model of car: “
INPUT model
cars = cars + 1
PRINT “Model # “, cars, “is “, model
END
ENDWHILE
NOTE: in the WHILE construct loop MUST be initialized before the counter variable
is incremented (increased).
WHILE loop
Flowchart

Read the marks of


students
terminated by
999. Find and
print the highest
mark.
Nested Conditions
 IF statements that are embedded into each other are said to be
nested. For every IF-THEN statement, there must be and ENDIF.
 Syntax:
IF <condition 1> THEN
<Action to be taken if condition 1 is true>
ELSE
IF <condition 2> THEN
<Action to be taken if condition 2 is met>
ELSE
IF <condition 3> THEN
<Action to be taken if condition 3 is met>
ELSE
<Action to be taken if condition 1 to 3 are not met>
ENDIF
ENDIF
ENDIF
Flowchart for
Sequence structure

 Problem 1: Read
in three marks and
display their
average.
Flowchart for
Selection Structure

Problem:
 Ask the user to
enter two
numbers then
display the
greater number.
Activity

 A company gives out bonuses based on the amount of income


generated by its sales representatives per month. Once the
income is greater than $5,000, a bonus of 10% of the
generated income is given to the employees. Read the income
generated and print the bonuses.
 Draw the flowchart to represent the information above.
Relational Operators
 Comparisons are made using a condition/decision
statement.
 A condition/decision statement is an expression that
when evaluated gives a Boolean result i.e. either
TRUE or FALSE.
 Condition statements use relational operators
between two variables e.g.
A>B
 or between a variable and a constant e.g.
age >= 50
Relational Operators
Relational Meaning
Operators
= Equal to

<> or ≠ Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

You might also like