Pseudocode: Examples of Pseudocode, How to write Pseudocode

Pseudocode is a programming tool that helps programmer design the problem before writing the program in a programming language. It is a detailed and easily understandable description of steps of algorithms or a program, which does not use any programming concepts, rather uses natural language. Program designer uses pseudocode as a programming tool to express and explain the design in depth which helps programmers understand the needs and converts that design into an actual program by writing codes in some programming language. Since it’s not a programming language; it cannot be compiled or executed. Unlike programming languages, there is no specific statements and syntax for writing Pseudo code.

Loading...

pseudocode

Advantages of pseudocode:

  • Pseudocode is easily written, read and understood
  • Its implementation is very useful in structured design elements.
  • It takes less time then drawing equivalent flowchart
  • Converting it to programming code is much easier as compared to flowchart
  • Can be easily modified when program’s logic needs modification

Disadvantages of Pseudocode:

Loading...
  • It is not visual
  • Not suitable for design
  • There is no standard format or style for writing, so one pseudocode may be different from another.
  • It is difficult to follow the logic and write pseudocode for a beginner and often gets confused with an algorithm.

How to write Pseudocode?

It’s actually pretty simple to write pseudocode, It’s like expressing your thoughts in clear English language. No need to worry about syntax rules or programming error. Just picture an overview of what needs to be achieved in your brain, and write it down on a piece of paper.

There are six basic computer operations

 

  1. A computer can receive information
           Read (information from a file)

           Get (information from the keyboard)

 

  1. A computer can put out information
           Write (information to a file)

           Display (information to the screen)

 

  1. A computer can perform arithmetic (* , + , – )
           Use actual mathematical symbols or the words for the symbols
                 Add number to total
                Total = total + number           
  1. A computer can assign a value to a piece of data.

     Three cases:

                To give data an initial value (initialize, Set)
                 To assign a value as a result of some processing (=)
                            x = 5 + y
                To keep a piece of information for later use
                       Save, Store

 

  1. A computer can compare two pieces f information and select one of two alternative actions
           IF condition THEN
                 some action
          ELSE
                 alternative action
          ENDIF

 

  1. A computer can repeat a group of actions
           WHILE condition (is true)
                  some action
           ENDWHILE
           FOR a number of times
                  some action
           ENDFOR

The Structure Theorem (and the pseudocode we use to represent the control structures).

It is possible to write any computer program by using only three basic control structures: sequence, selection, repetition. Selection includes IF-THEN-ELSE and CASE and FOR…NEXT, WHILE ENDWHILE comes under Repetition.

 Sequence: In a sequential method of writing pseudocode,  Steps are executed one after another (top –down approach). This is represented as a sequence of pseudocode statements:

            Statement 1
            Statement 2
            Statement 3

Example:

          Read three numbers

            Add three numbers

            Display total of three numbers

SELECTION (IF-THEN-ELSE):

Presentation of a condition and the choice between two actions, the choice depending on whether the condition is true or false.  This construct represents the decision-making abilities of the computer to compare two pieces of information and select one of two alternative actions.  In pseudocode, The selection is represented by the keywords IF, THEN, ELSE, ENDIF, SELECT, END SELECT.

General Structure:

            IF condition p is true THEN
                        statement(s) in true case
            ELSE
                        statement(s) in false case
            ENDIF

Example:

            IF student is part_time THEN
                        Add one to part_time_count
            ELSE
                        Add one to full_time_count
            ENDIF

A variation – We don’t need the ELSE structure – The null ELSE

 

             IF condition p is true THEN

                statement(s) in true case

             ENDIF

SELECTION (CASE):

case in pseudocode

The case structure in pseudocode indicates multiple ways based on the conditions that are mutually exclusive. Keywords like OF, OTHERS, CASE can be used to represent alternatives.  OTHERS can be used to indicate default sequence or statement when none of the given conditions satisfies the test expression. The general form of CASE selection is:

SELECT CASE (text expression)

Condition 1:
     Statements 1
Condition 2:
      Statement  2
Condition 3:
      Statement 3
.....
Condition n:
     Sequence n
OTHERS:
Default statement
ENDSELECT

Example:

CASE  Title of
                Dr         : Display “doctor”
                MR         : Display  “Mister”
                Miss       : Display  “Miss”
Example:
CASE division of
                Second division               : Percenatge>50 and percentage <60
                First division                : percentage>=60 and Percentage <80
                Distinction division          : percentage >=80

REPETITION (WHILE):

Presentation of a set of instructions to be performed repeatedly, as long as a condition is true. It specifies a loop with a test condition at the top. WHILE indicates the beginning of the loop and ENDWHILE indicates the ending of the loop. The general form of WHILE …. ENDWHILE is:

            WHILE condition p is true
                      Statement(s) to execute
            ENDWHILE

 

The condition is tested before any statements are executed.  It is imperative that at lease one statement within the statement block alter the condition and eventually render it false, otherwise the logic may result in an endless loop.

 

Example:

            Set student_total to 0
            WHILE student_total < 50
                        Read student record
                        Print student name and address
                        Add 1 to student_total
            ENDWHILE

Note: The variable student_total is initialized before the loop condition is executed.  The student_total variable is incremented within the body of the loop so it will eventually stop.  These are both essential features of the WHILE construct.

REPETITION (FOR):

This loop repeats a statement or statement block several times. FOR and ENDFOR are two keywords used.

Basic structure of FOR:

                FOR iteration range  
                             Sequence/statements
                Endfor

Example:

FOR I = 1 to 10
Display i
ENDFOR

Let’s understand more with Examples of Pseudocode.

Pseudocode to add two numbers:

The First step is to analyze the requirements, and then understand the process that is needed to get the result. So for this example, since we are required to add two numbers, The first number and second numbers are our requirements, and the process is an addition which is the result of adding the two numbers, finally it ends with displaying the result.

Read first number say f and second number say s
Add ‘s’ and ‘f’ and store the result In Sum
Print sum

Common Action Keywords in Pseudocode.

There are numbers of various keywords that are used to show common input, output and processing operations.

To indicate:

INPUT: OBTAIN, GET, READ

OUTPUT: DISPLAY, SHOW, PRINT

COMPUTE: COMPUTE, CALCULATE, DETERMINE

INITIALIZE: SET, INIT

ADD ONE: INCREMENT, BUMP

keywords used in pseudocode

Loading...
Malcare WordPress Security