Algorithm Design

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Topic: Algorithm Design Using

Pseudocode
Algorithm
•A process or set of rules to be followed in calculations or other
problem-solving operations, especially by a computer is known as
Algorithm.

•Algorithm can be designed by using Pseudocode ,Flowchart and


standard General English.

•A flowchart is a formalized graphic representation of a logic


sequence, work or manufacturing process, organization chart, or
similar formalized structure. The purpose of a flow chart is to
provide people with a common language or reference point when
dealing with a project or process.
PEUDOCODE
• Pseudocode  is a detailed readable description
of what a computer program or algorithm
must do, expressed in a formally-styled natural
language rather than in
a programming language. 

• Pseudocode is sometimes used as a detailed


step in the process of developing a program.
IDENTIFIER
• Identifiers (the names given to variables, constants,
procedures and functions) are in mix case. They can
only contain letters (A–Z, a–z) and digits (0–9).

• They must start with a letter and not a digit. Accented


letters and other characters, including the underscore,
should not be used.

• As in programming, it is good practice to use identifier


names that describe the variable, procedure or
function they refer to.
VARIABLE
• A variable is a data structure which stores a single value that can
change, depending on conditions or on information passed to the
program. 
• It is good practice to declare variables explicitly in pseudo code.
• Declarations are made as follows:

DECLARE <identifier> :<data type>

Example – variable declarations

DECLARE Counter : INTEGER


DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
CONSTANT
• A constant is a value that cannot be altered by the program during normal execution,
i.e., the value is constant.

• It is good practice to use constants if this makes the pseudocode more readable, as an
identifier is more meaningful in many cases than a literal. It also makes the
pseudocode easier to update if the value of the constant changes.

• Constants are normally declared at the beginning of a piece of pseudocode (unless it is


desirable to restrict the scope of the constant).

• 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”
ASSIGNMENT
• 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 or an abstract data type).

• 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
ARITHMETIC OPERATOR
Standard arithmetic operator symbols are used:
+ Addition
- Subtraction
* Multiplication
/ Division
• Care should be taken with the division operation: the resulting value should
be of data type REAL, even if the operands are integers.

• The integer division operators MOD and DIV can be used. However, their use
should be explained explicitly and not assumed.

• 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 OPERATOR
• The following symbols are used for relational operators (also
known as comparison operators):
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= 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 OPERATOR
• The only logic operators (also called relational
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.
COUNTING
• Counting is used to find how many numbers/items there are in a list.

• Counting in 1s is quite simple; use of the statement count = count +


1 will enable counting to be done (e.g. in controlling a repeat loop).

• The statement literally means:


(new) count = the (old) count + 1

• It is possible to count in any increments just by altering the


numerical value in the statement (e.g. count = count – 1 counts
backwards) .

• Counting is used to find how many numbers/items there are in a list.


TOTALLING
• Totalling is used to sum a list of numbers.
• To add up a series numbers the following type
of statement should be used:
• total = total + number
• This literally means (new) total = (old) total +
value of number
• Totalling is used to sum a list of numbers.
INPUT/OUTPUT Statement
• INPUT and OUTPUT are used for the entry of data and display
of information.
• Sometimes READ can be used instead of INPUT; this is usually
used for reading from files, which is not covered in this
textbook.
• Frequently PRINT is used instead of OUTPUT. INPUT is used for
data entry.
BRANCHING/CONDITIONAL/SELECTION STATEMENT

• Selection decides which statement(s) are to be


executed depending upon the result of a question.

There are two common ways of branching:

• CASE … OF … OTHERWISE … END CASE /


CASE … OF … END CASE

• IF … THEN … ELSE … END IF /


IF … THEN … END IF
IF … THEN … ELSE … ENDIF
IF … THEN … ELSE … ENDIF:
• For an IF condition the THEN path is followed if the condition is TRUE and the 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.
• IF statements with an else clause are written as follows:
IF <condition> THEN
<statements> ELSE
<statements>
ENDIF
• Example:
IF MyValue > YourValue THEN
OUTPUT “ I will Win”
ELSE
OUTPUT “ You will Win”
END IF
IF … THEN … ENDIF
IF … THEN … ENDIF:
• An IF statement is a programming conditional statement that,
if proved true, performs a function or displays information. 
The end of the statement is shown by ENDIF.
• IF statements without an else clause are written as follows:
IF <condition> THEN
<statements>
ENDIF
• Example:
IF MyValue > YourValue THEN
OUTPUT “ I will Win”
END IF
CASE … OF … OTHERWISE … END CASE
• One single statement contains multiple section. For a CASE condition the value
of the variable decides the path to be taken. Several values are usually
specified.
• OTHERWISE is the path taken for all other values. The end of the statement is
shown by ENDCASE.
CASE Multiple Choice without Alternative CASE Multiple Choice with Alternative
CASE OF <identifier> CASE OF <identifier>
<value 1> :<statement> <value 1> :<statement>
<value 2> :<statement>
<value 2> :<statement> ...
... OTHERWISE <statement>
ENDCASE ENDCASE
Case Statement VS IF Statement
IF Statement CASE Statement

INPUT number INPUT number


IF number = 1 then CASE number of
x=x+1 1: x = x + 1
ELSE IF number = 2 then 2: y = y + 1
y=y+1 OTHERWISE: PRINT “error”
ELSE ENDCASE
PRINT “error”
END IF
LOOP/ITERATION/REPETETION
There are three types of loop.
Count Controlled Loop:
• FOR… TO … NEXT
Conditional Loop
• WHILE …DO… END WHILE [Pre-conditional Loop]
• REPEAT … UTILL [Post-Conditional Loop]
FOR … TO … NEXT
• A set number of repetitions.
• Count Control Loop.
• A variable is set up with a start value and an end value and then incremented
in steps of one until the end value is reached and the iteration finishes.
• The variable can be used within the loop so long as its value is not changed.
• This type of loop is very useful for reading values into lists.

FOR <identifier><value1> TO <value2> STEP <increment>


<statements>
NEXT
TOTAL=0
FOR COUNT  1 TO 10
INPUT NUMBER
TOTAL=TOTAL+NUMBER
NEXT COUNT
AVERAGE=TOTAL/10
OUTPUT AVERAGE
WHILE … DO… END WHILE
• Pre-Conditional loop
• A repetition, where the number of repeats is not known, that may never be
completed.
• The actions are only repeated WHILE a given condition is true.
• The action in the loop will continue until the condition is False.

WHILE <condition> DO

<statements>
ENDWHILE
INPUT NUM
WHILE NUM >9 DO
NUM=NUM-9
END WHILE
REPEAT… UNTIL
• Post Conditional Loop.
• The actions are only repeated WHILE a given condition is False.
• The action in the loop will continue until the condition is True.
• The actions in this loop are always completed at least once.

REPEAT
<Statements>
UNTIL <condition>

INPUT NUM
REPEAT
NUM=NUM-9
UNTIL NUM<9
Difference between
WHILE … DO… END WHILE and REPEAT… UNTIL
Comparison between FOR … TO … NEXT, WHILE … DO… ENDWHILE and REPEAT… UNTIL
Comparison of different Loops using Pseudocode
Features to make a pseudo code more
understandable:

• Indentation
• Meaningful identifier Names
• Comments.
Sequence
Sequence is the concept of one statement being
executed after another. Example:
INPUT NUM1
INPUT NUM2
TOTAL=NUM1+NUM2
OUTPUT TOTAL
Frequently Asked Question
Q1. Write the names of two Selection or conditional
statement.
Q2. Write the names of three loop structure.
Q3. Explain two selection statements with example.
Q4. Explain three loops with example.
Q5. Write names of arithmetic and relational operation.
Q6. List three logical operators.
Q7. How to make a program more understandable?
Q8. Explain Counting, Totaling, Sequence and Selection with
example.

You might also like