Professional Documents
Culture Documents
Chapter 7 - Part2
Chapter 7 - Part2
Using Pseudocode
Pseudocode is a simple method of showing an algorithm. It describes what the
algorithm does by using English key words that are very similar to those used in a high-
level programming language.it is not written on the computer to run
A constant in a computer program is a named data store than contains a value that
does not change during the execution of a program. As with variables,
The pseudocode statements
1. Assignments statements
3. Conditional Statements
4. Loop Statements
The pseudocode statements- Assignment statement
Assignment statement : A value is assigned to an item/variable using the operator. .
The variable on the left of the is assigned the value of the expression on the right.
Is equivalent to the processing shape in the flowchart .
Mathematical operators :
results.
• For a program to be useful, the user needs to use prompt messages to know
• For an IF condition the THEN path is followed if the condition is true and
• ELSE path is followed if the condition is false.
• There may or may not be an ELSE path.
• The end of the statement is shown by ENDIF.
Example : Have a look at the algorithm below that specifies what happens if the value of
Choice is 1, 2, 3 or 4.
CASE OF Choice
1 : Answer ← Num1 + Num2
2 : Answer ← Num1 - Num2
3 : Answer ← Num1 * Num2 4 : Answer ← Num1 / Num2
OTHERWISE
OUTPUT "Please enter a valid choice"
ENDCASE
Loop structures (iteration )
When some actions performed as part of an algorithm need repeating, this is called
‘iteration’.
Why using loops : Loop structures are used to perform the iteration.
There are three different types of loop structure It is important to know though that not all
loops work in the same way, and we are expected to know which loop to use and
when.
General Kinds of Repetition
Fixed repetition: (For Loop)
It can be determined in advance how many times a segment of code will be
repeated
The number of times the segment of code is repeated is independent of what
happens inside the loop
Syntax:
REPEAT
<loop body>
UNTIL <termination condition>
• This loop structure is used when the number of repetitions/iterations is not known
• The actions are only repeated WHILE a given condition is true.
• If the WHILE condition is False when the loop is first entered then the actions in
the loop are never performed.
• If condition evaluates to
false, loop exit occurs, and next program statement is executed
True , loop-body is repeated
Common Algorithms
– linear search
– bubble sort
– totaling
– counting
– finding maximum, minimum and average values
Common Algorithms- totaling and finding the average
Example : The flowchart shows an algorithm that should:
• allow 100 numbers to be entered into the variable Number
• total the numbers as they are entered
• output the total and average of the numbers after they have all been entered.
Variables :
• Counter to count 100 numbers must be initialized to 0
• Number : saves the input values
• Total : saves the total : initialize it to 0 and use the formula : Total total + Number
• Average Total / 100 OR Total /counter
0478/22/M/J/21
Example : Counting
Keeping a count of the number of times an action is performed is another standard method.
For example, counting the number of students that were awarded a pass mark:
PassCount ← 0
FOR Counter ← 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50 THEN
PassCount ← PassCount + 1
NEXT Counter
Write An algorithm in pseudocode to input 50 numbers. Count how many positive numbers and how many negative
numbers.