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

Sequence and Selection Control Structure

The document outlines the structure and syntax rules for writing pseudocode, including indentation, data types, literals, identifiers, and control structures. It provides examples for variable and constant declarations, array usage, arithmetic and relational operators, and input/output operations. Additionally, it covers selection control structures such as IF and CASE statements with practical pseudocode examples for various scenarios.

Uploaded by

nishaaneducation
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 views

Sequence and Selection Control Structure

The document outlines the structure and syntax rules for writing pseudocode, including indentation, data types, literals, identifiers, and control structures. It provides examples for variable and constant declarations, array usage, arithmetic and relational operators, and input/output operations. Additionally, it covers selection control structures such as IF and CASE statements with practical pseudocode examples for various scenarios.

Uploaded by

nishaaneducation
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/ 11

Indentation

Lines are indented by four spaces to indicate that they are contained within a statement in a
previous line. Where it is not possible to fit a statement on one line any continuation lines are
indented by two spaces from the margin. In cases where line numbering is used, this
indentation may be omitted. Every effort will be made to make sure that code statements are
not longer than a line of code, unless this is necessary. Note that the THEN and ELSE clauses
of an IF statement are indented by only two spaces. Cases in CASE statements are also
indented by only two spaces.

Case
Keywords are in upper case, e.g. IF, REPEAT, PROCEDURE. Identifiers are in mixed case
with upper case letters indicating the beginning of new words, e.g. NumberOfPlayers.
Meta-variables – symbols in the pseudocode that should be substituted by other symbols are
enclosed in angled brackets < >.

Basic data types


The following keywords are used to designate basic data types:
• INTEGER a whole number
• REAL a number capable of containing a fractional part
• CHAR a single character
• STRING a sequence of zero or more characters
• BOOLEAN the logical values TRUE and FALSE

Literals
Literals (values) of the above data types are written as follows:
• Integer written as normal in the denary system, e.g. 5, –3
• Real always written with at least one digit on either side of the decimal point, zeros being
added if necessary, e.g. 4.7, 0.3, –4.0, 0.0
• Char a single character delimited by single quotes, e.g. ꞌxꞌ, ꞌcꞌ, ꞌ@ꞌ
• String delimited by double quotes. A string may contain no characters (i.e. the empty
string), e.g. "This is a string", ""
• Boolean TRUE, FALSE
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mixed
case using Camel case, e.g. FirstName. They can only contain letters (A–Z, a–z) and digits
(0–9). They must start with a capital letter and not a digit. Accented letters and other
characters, including the underscore, should not be used. Keywords cannot be used as
identifiers.

Data Structures:
Variables:
Named data store whose value can change during the execution of the program.
Variable declarations
Declarations are made as follows:
DECLARE <identifier> : <data type>
Example – variable declarations
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN

Constant:
Named data store whose value cannot change during the execution of the program.
It is good practice to use constants if this makes the pseudocode more readable, and easier to
update if the value of the constant changes. Constants are declared by stating the identifier
and the literal value in the following format:
CONSTANT <identifier> ← <value>
Example – CONSTANT declarations
CONSTANT HourlyRate ← 6.50
CONSTANT DefaultText ← "N/A"
Only literals can be used as the value of a constant. A variable, another constant or an
expression must never be used.
Arrays
Arrays are fixed-length structures of elements of identical data type, accessible by
consecutive index numbers. It is good practice to explicitly state what the lower bound of the
array (i.e. the index of the first element) is because this defaults to either 0 or 1 in different
systems. Generally, a lower bound of 1 will be used. Square brackets are used to indicate the
array indices.
Declaring arrays
1D and 2D arrays are declared as follows (where l, l1, l2 are lower bounds and u, u1, u2 are
upper bounds):
DECLARE <identifier> : ARRAY[<l>:<u>] OF <data type>
DECLARE <identifier> : ARRAY[<l1>:<u1>,<l2>:<u2>] OF <data type>
Example – array declaration
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Using arrays in the main pseudocode statements, only one index value is used for each
dimension in the square brackets.
Example – using arrays
StudentNames[1] ← "Ali"
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]

Arithmetic Operator:
+ addition
– subtraction
* multiplication
/ division
^ raised to the power of
The integer division operators MOD and DIV can also be used.
DIV(<identifier1>,<identifier2> ) Returns the quotient of identifier1 divided by identifier2
with the fractional part discarded. MOD(<identifier1>,<identifier2>) Returns the remainder
of identifier1 divided by identifier2 The identifiers are of data type integer.
Examples – MOD and DIV
DIV(10, 3) returns 3
MOD(10, 3) returns 1
Multiplication and division have higher precedence over addition and subtraction (this is the
normal mathematical convention). However, it is good practice to make the order of
operations in complex expressions explicit by using parentheses

Relational Operators: Logical operators


The following symbols are used for logical operators:
= equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
<> not equal to
The result of these operations is always of data type BOOLEAN. In complex expressions, it
is advisable to use parentheses to make the order of operations explicit

Logical Operators: Boolean operators


The only Boolean operators used are AND, OR and NOT. The operands and results of these
operations are always of data type BOOLEAN. In complex expressions, it is advisable to use
parentheses to make the order of operations explicit

Assignments
The assignment operator is ←. Assignments should be made in the following format:
<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure
such as an array). The value may be any expression that evaluates to a value of the same data
type as the variable. Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Precedence of Operators
()
^
*, /, MOD,DIV
+, -
Relational operators
NOT
AND
OR

Input and output


Values are input using the INPUT command as follows:
INPUT <identifier>
The identifier should be a variable (that may be an individual element of a data structure such
as an array).
Values are output using the OUTPUT command as follows:
OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.
Examples – INPUT and OUTPUT statements
INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"

Sequence Control Structure:


All the statements are executed in the predefined sequence.
LET A  5
LET B  10
Sum  A + B
OUTPUT “Sum = “, Sum

OUTPUT: Sum = 15
1. Write pseudocode to find sum of two numbers entered by user.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
Sum  Num1 + Num2
OUTPUT “Sum = “, Sum

2. Write pseudocode to find average of two numbers entered by user.


OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
Average  (Num1 + Num2) / 2
OUTPUT “Average = “, Average

Write pseudocode to find area and perimeter of a rectangle. Length and width will be entered
by user.
OUTPUT “Enter length”
INPUT Length
OUTPUT “Enter Width”
INPUT Width
Area  Length * Width
Perimeter  2 * (Length + Width)
OUTPUT “Area of rectangle = “, Area
OUTPUT “Perimeter of rectangle = “, Perimeter

3. Write pseudocode to find area of a circle. Radius of circle will be entered by user.
CONSTANT Pi  3.142
OUTPUT “Enter radius”
INPUT Radius
Area  Pi * Radius ^ 2
OUTPUT “Area of circle = “, Area
4. Write pseudocode to find square and square root of a number entered by user.
OUTPUT “Enter a number”
INPUT Num
Square  Num ^ 2
SquareRoot  Num ^ (½)
OUTPUT “Square = “, Square
OUTPUT “Square root = “, SquareRoot

Selection Control Structure:


Execution of statements depends upon outcome of a condition or value of some expression.
e.g.
1. IF ---- THEN ----- ELSE ----- END IF
2. CASE ---- OF ----- OTHERWISE ----- END CASE

IF ---- THEN ----- ELSE ----- END IF:


Also called decision statement. Used to select between two alternatives. Execution of the
statements depends upon outcome of a condition. Some statements are executed and some are
skipped. THEN part is executed IF the condition is true and ELSE part is executed IF the
condition is FALSE.
IF statements may or may not have an ELSE clause. IF statements without an ELSE clause
are written as follows:
IF <condition>
THEN
<statements>
ENDIF
IF statements with an ELSE clause are written as follows:
IF <condition>
THEN
<statements>
ELSE
<statements>
ENDIF
Note that the THEN and ELSE clauses are only indented by two spaces. (They are, in a sense,
a continuation of the IF statement rather than separate statements.) When IF statements are
nested, the nesting should continue the indentation of two spaces.

5. Write pseudocode which inputs two numbers and outputs the larger number.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
IF Num1 > Num2
THEN
OUTPUT Num1, “ is larger.”
ELSE
OUTPUT Num2, “ is larger.”
END IF

6. Write pseudocode which inputs a number and output whether the number is positive or
not.
OUTPUT “Enter a number”
INPUT Num
IF Num > 0
THEN
OUTPUT Num, “is positive”
ELSE
OUTPUT Num, “is not positive”
END IF

7. Write pseudocode which inputs a number and output whether the number is even or odd.
OUTPUT “Enter a number”
INPUT Num
IF MOD (Num,2) = 0
THEN
OUTPUT Num, “is even”
ELSE
OUTPUT Num, “is odd”
END IF
8. Write Pseudocode which inputs three numbers from the user and outputs the largest
number.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
OUTPUT “Enter third number”
INPUT Num3
IF Num1 > Num2 AND Num1 > Num3
THEN
OUTPUT Num1, “ is the largest number.”
ELSE
IF Num2 > Num3
THEN
OUTPUT Num2, “ is the largest number.”
ELSE
OUTPUT Num3, “ is the largest number.”
END IF
END IF

CASE ---- OF ---- OTHERWISE ----- END CASE


Is a multiple branching statement. Used to select between multiple discrete alternatives.
CASE statements allow one out of several branches of code to be executed, depending on the
value of a variable.

CASE OF <identifier>
<value1>: <Statement>
<value2>: <Statement>
<value3>: <Statement>

OTHERWISE: Statement
END CASE
It is best practice to keep the branches to single statements as this makes the pseudocode
more readable. Similarly, single values should be used for each case. If the cases are more
complex, the use of an IF statement, rather than a CASE statement, should be considered.
Each case clause is indented by two spaces. They can be considered as continuations of the
CASE statement rather than new statements. Note that the case clauses are tested in sequence.
When a case that applies is found, its statement is executed, and the CASE statement is
complete. Control is passed to the statement after the ENDCASE. Any remaining cases are
not tested.
If present, an OTHERWISE clause must be the last case. Its statement will be executed if
none of the preceding cases apply

9. Write pseudocode which inputs day number(1-7) and outputs corresponding day name e.g
1 is Monday.
OUTPUT “Enter day number (1-7)”
INPUT DayNum
CASE OF DayNum
1: OUTPUT “Monday”
2: OUTPUT “Tuesday”
3: OUTPUT “Wednesday”
4: OUTPUT “Thursday”
5: OUTPUT “Friday”
6: OUTPUT “Saturday”
7: OUTPUT “Sunday”
OTHERWISE OUTPUT “Invalid day number”
END CASE
10. Write pseudocode which inputs two numbers and an operator (+, -, *, /) and output the
result according to the operator selected by user using CASE OF statement.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
OUTPUT “Enter operator (+,-,*,/) ”
INPUT Op
CASE OF Op
“+”: OUTPUT “Sum = “, Num1 + Num2
“-”: OUTPUT “Difference = “, Num1 – Num2
“*”: OUTPUT “Product = “, Num1 * Num2
“/”: OUTPUT “Division Answer = “, Num1 / Num2
OTHERWISE: OUTPUT “Invalid operator”
END CASE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy