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

Operators Used in Pseudocode Assignment Statements

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

Operators Used in Pseudocode Assignment Statements

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

Operators used in pseudocode assignment statements

Logic Statements

Selection construct uses a condition to follow either first group of steps or second group of steps.
Condition consists of at least one logic proposition.

Logic propositions use relational (comparison) Relational operators shown in table.

Person is classed as child if they are under 13 and adult if they are over 19. If they are between 13 and
19 inclusive they are classed as teenagers. We can write these statements as logic statements:

• If Age < 13 then person is a child

• If Age > 19 then person is an adult

• If Age >= 13 AND Age <= 19 then person is a teenager

What is meant by Transferable skill?

Knowledge or experience of one programming language can be applied to another or unfamiliar or an


unknown computer language.

Example: It will help to recognize control structures of unknown language and also will help them learn a
new computer language.

CS- P2 Page 1
Features that make pseudocode or program easier to read and understand:

Good programming practices:

✓ Meaningful or sensible identifier names

✓ Use of Camel case fonts for identifier names

✓ Capitalization of Keywords

✓ Use of (library/built-in) functions

✓ Use of constants

✓ Use of Indentation

✓ Use of blank lines and white space where appropriate

✓ Add comments

Purpose of adding comments while writing pseudocode or program

Comments are used in program or pseudocode to improve its readability. Comments are not compiled
(translated) therefore not executed, rather they help programmer in understanding code. Comments are
preceded by two forward slashes // in pseudo-code.

Stepwise Refinement

Many problems that we want to solve are complex. To make it easier to solve a complex problem, we
break the problem down into smaller steps. These might need breaking down further until the steps are
small enough to solve easily. This process of breaking down is known as stepwise refinement.

Types of Variable

Local variable: a variable that is accessible only within the module in which it is declared. Good design
uses local variables as it makes modules independent and re-usable.

Global variable: a variable that is accessible from all modules.

Q: Take two number from user and swap their values.

Solution: DECLARE NUM1, NUM2, Temp : Integer

PRINT “Enter two number”

INPUT NUM1 , NUM2

Temp←NUM1

NUM1←NUM2

NUM2←Temp

PRINT NUM1 , NUM2

CS- P2 Page 2
9.2.3 Writing pseudocode from a structured English

description

There are no set rules for writing structured English – the wording just needs to be unambiguous

and easily understandable. Pseudocode is more precise and usually follows an agreed set of rules.

From a structured English description, the following things need to be possible:

• Any variables that need to be used can be identified and put in an identifier table – these can be

items input or output as the results of calculations.

• Input and output can be identified from the wording used, for example, Enter, Read, Print,

Write.

• Selection can be identified from the wording used, for example, If, Then, Choose.

• Any iteration required can be identified from the wording used, for example, Loop, Repeat.

• Any processes needed can be identified from the wording used, for example, Set, Calculate.

When the identifier table is complete, each structured English statement can be used to write one

or more pseudocode statements, keeping the same order as the structured English.

Here is an example of an algorithm to calculate a runner’s marathon time in seconds, using

structured English.

1 Enter time taken to run marathon in hours, minutes and seconds

2 Calculate and store marathon time in seconds

3 Output marathon time in seconds

This can be used to identify the variables required and complete the identifier table (Table 9.3).

Identifier name Description

MarathonHours The hours part of the marathon time

MarathonMinutes The minutes part of the marathon time

MarathonSeconds The seconds part of the marathon time

TotalMarathonTimeSeconds Total marathon time in seconds

CS- P2 Page 3
Table 9.3 Using these identifiers, each step of the structured English algorithm can be converted to

pseudocode, as demonstrated below.

1 Enter time taken to run marathon in hours, minutes and seconds

There are three variables used: MarathonHours, MarathonMinutes and MarathonSeconds. This is

explicitly input and implicitly output as the user needs to understand what input is required. The

2 Calculate and store marathon time in seconds

This is a process using the variables MarathonHours, MarathonMinutes and MarathonSeconds

and using an assignment statement to store the result in TotalMarathonTimeSeconds. The

pseudocode required is as follows.

3 Output marathon time in seconds

This is output using the variable TotalMarathonTimeSeconds. The pseudocode required is as

follows.

CS- P2 Page 4
ACTIVITY 9G

The structured English description has been extended below to check the runner’s time against

their personal best.

1 Enter time taken to run marathon in hours, minutes and seconds

2 Calculate and store marathon time in seconds

3 Output marathon time in seconds

4 Enter personal best time in seconds

5 If marathon time in seconds is shorter than the personal best time then

6 Reset personal best time in seconds

7 Output the personal best time

Extend the identifier table and write the extra pseudocode to complete the algorithm. Then check your
algorithm works by writing a short program from your pseudocode statements

using the same names for your identifiers

9.2.4 Writing pseudocode from a flowchart

Flowcharts are diagrams showing the structure of an algorithm using an agreed set of symbols, as

shown in Table 9.4.

CS- P2 Page 5
Table 9.4

Flowcharts can be used to identify any variables required and you can then complete an identifier

table. Each flowchart symbol can be used to identify and write one or more pseudocode

statements.

Here is an example of a flowchart of an algorithm that can be used to check an exam grade:

CS- P2 Page 6
Figure 9.4

The same algorithm is presented in pseudocode on the left. Below is the identifier table:

CS- P2 Page 7
9.2.5 Stepwise refinement

The algorithms looked at so far have been short and simple. When an algorithm is written to

solve a more complex problem, decomposition is used to break the problem down into smaller

and more manageable parts. These parts then need to be written as a series of steps where each

step can be written as a statement in a high-level programming language, this process is called

stepwise refinement.

Many problems are more complex than they seem if a robust solution is to be developed. Look at

the first step of the structured English to calculate a time in seconds.

CS- P2 Page 8
1 Enter time taken to run marathon in hours, minutes and seconds

2 Calculate and store marathon time in seconds

3 Output marathon time in seconds

The first step can be further broken down, as follows:

1.1 Enter the hours

1.2 Enter the minutes

1.3 Enter the seconds

ACTIVITY 9H

The flowchart on page 232 has been extended to allow more than one mark to be input.

CS- P2 Page 9
check your algorithm works by writing a short program from your pseudocode statements

using the same names for your identifiers.

Each of these steps can be broken down further:

1.1.1 Input value for hours

1.1.2 Check input in the range 2 to 8

1.1.3 Reject if out of range or not a whole number and re-input value step 1.1.1

1.1.4 Accept and store value in hours

CS- P2 Page 10
1.2.1 Input value for minutes

1.2.2 Check input in the range 0 to 59

1.2.3 Reject if out of range or not a whole number and re-input value step 1.2.1

1.2.4 Accept and store value in minutes

1.3.1 Input value for seconds

1.3.2 Check input in the range 0 to 59

1.3.3 Reject if out of range or not a whole number and re-input value step 1.3.1

1.3.4 Accept and store value in seconds

These steps can now be written in pseudocode. For example, the input routine for the seconds:

ACTIVITY 9I

Look at the algorithm to calculate the area of a chosen shape written in structured English

below. Use stepwise refinement to break each step into more manageable parts then rewrite the

algorithm using pseudocode.

1 Choose the shape (square, triangle, circle)

2 Enter the length(s)

3 Calculate the area

4 Output the area

Then check your pseudocode algorithm works by writing a short program from your

pseudocode statements using the same names for your identifiers.

CS- P2 Page 11
pseudocode required is as follows. Selection / Conditional / Decision Statements

CS- P2 Page 12

You might also like