Pseudocode

 

PART 1: INTRODUCTION


A prototype is an early sample, model or release of a product created with the intention of concept testing and for learning purposes. They help us to learn without fully implementing our solutions. When developing user interfaces for our applications, we have several prototypes before the final interface. Some examples of these are wire-frames, graphical designs and mock-ups. The same applies to writing technical code. Directly writing code for complex purposes might result in time wastage. The causes of this range from improper algorithms to ambiguous program flow. To prevent this, we can use Pseudocode.
Why use Pseudocode?

What is Pseudocode?

Pseudocode is an informal high-level description of a computer program or algorithm. It is written in symbolic code which must be translated into a programming language before it can be executed.

Are there alternatives to Pseudocode?

There are some alternatives to Pseudocode. Some of them are Flowcharts, drakon-charts and Unified Modified Language (UML) charts. They will serve the purpose but they comparatively require more resources.

PART 2: STATEMENTS

A statement is defined as an instruction that directs the computer to perform a specific action. In writing pseudocode, we will refer to singular instructions as statements.

When writing pseudocode, we assume that the order of execution of the statements is from top to bottom. This changes when using control structures, functions and exception handling.

Mathematical operations

Mathematical operations are integral to solution development. They allow us to manipulate the values we have stored. Here are common mathematical symbols:

Assignment: ← or :=    Example: c ← 2πr, c := 2πr Comparison: =, ≠, <, >, ≤, ≥Arithmetic: +, −, ×, /, modFloor/ceiling: ⌊, ⌋, ⌈, ⌉a ←    ⌊b⌋    + ⌈cLogical: and, orSums, products: Σ Π    Example: h ←    ΣaA    1/a

Keywords

A keyword is a word that is reserved by a program because the word has a special meaning. Keywords can be commands or parameters. Every programming language has its own keywords (reserved words). Keywords cannot be used as variable names.

In Pseudocode, they are used to indicate common input-output and processing operations. They are written fully in uppercase.

START: This is the start of your pseudocode.INPUT: This is data retrieved from the user through typing or through an input device.READ / GET: This is input used when reading data from a data file.PRINT, DISPLAY, SHOW: This will show your output to a screen or the relevant output device.COMPUTE, CALCULATE, DETERMINE: This is used to calculate the result of an expression.SET, INIT: To initialize valuesINCREMENT, BUMP: To increase the value of a variableDECREMENT: To reduce the value of a variable

variable (named location to hold data and can change whilst the program is running)


  • Datatypes:
integer: A whole number e.g. 1,123
float: A number with contains a decimal e.g. 4.7,-1.23
char: A single character e.g. x, @, $
string: A sequence of zero or more characters, uses the ""
boolean: The logical values TRUE and FALSE

    PART 3: CONDITIONALS

    During algorithm development, we need statements which evaluate expressions and execute instructions depending on whether the expression evaluated to True or False. Here are some common conditions used in Pseudocode:

    IF — ELSE IF — ELSE

    This is a conditional that is used to provide statements to be executed if a certain condition is met. This also applies to multiple conditions and different variables.

    Here is an if statement with one condition

    IF you are happy
    THEN smile
    ENDIF

    Here is an if statement with an else section. Else allows for some statements to be executed if the “if” condition is not met.

    IF you are happy THEN
    smile
    ELSE
    frown
    ENDIF

    We can add additional conditions to execute different statements if met.

    IF you are happy THEN
    smile
    ELSE IF you are sad
    frown
    ELSE
    keep face plain
    ENDIF

    (allows program to follow different paths with one value, we use IF/ELSE/ELIF/nested if which is an if within an if, you still have to use indentation, and end the IF function using EndIF)

         IF Weight < 70 THEN
            Print "You can continue as normal"
         ELSE
         ENDIF
         IF Weight > 70 THEN
            PRINT "You need to start exercising more"
         ENDIF

    CASE statements are a method of selection when answers don’t consist of values between a particular range or there are specific values. We use CASE/OF/OTHERWISE/ENDCASE

    CASE

    Case structures are used if we want to compare a single variable against several conditions.

    INPUT colorCASE color of    red: PRINT "red"
    green: PRINT "green"
    blue: PRINT "blue"
    OTHERS
    PRINT "Please enter a value color"
    ENDCASE

    The OTHERS clause with its statement is optional. Conditions are normally numbers or characters

    PART 4: ITERATION

    To iterate is to repeat a set of instructions in order to generate a sequence of outcomes. We iterate so that we can achieve a certain goal.

    FOR structure

    The FOR loop takes a group of elements and runs the code within the loop for each element.

    FOR every month in a year    Compute number of daysENDFOR

    WHILE structure

    Similar to the FOR loop, the while loop is a way to repeat a block of code as long as a predefined condition remains true. Unlike the FOR loop, the while loop evaluates based on how long the condition will remain true.

    To avoid a scenario where our while loop runs infinitely, we add an operation to manipulate the value within each iteration. This can be through an increment, decrement, et cetera.

    PRECONDITION: variable X is equal to 1
    WHILE Population < Limit
    Compute Population as Population + Births — DeathsENDWHILE

    PART 5: FUNCTIONS

    When solving advanced tasks it is necessary to break down the concepts in block of statements in different locations. This is especially true when the statements in question serve a particular purpose. To reuse this code, we create functions. We can then call these functions every-time we need them to run.

    Function clear monitor
    Pass In: nothing
    Direct the operating system to clear the monitor
    Pass Out: nothing
    Endfunction

    To emulate a function call in pseudocode, we can use the Call keyword

    call: clear monitor

    PART 6: PROGRAM WRAPPING

    After writing several functions in our pseudocode, we find the need to wrap everything into one container. This is to improve readability and make the execution flow easier to understand.

    To do this, we wrap our code as a program. A program can be defined as a set of instructions that performs a specific task when executed.

    PROGRAM makeacupofteaEND

    PART 7: EXCEPTION HANDLING

    An exception is an event which occurs during program execution that disrupts the normal flow of the instructions. These are events that are non-desirable.

    We need to observe such events and execute code-blocks in response to them. This is called exception handling.

    BEGIN 
    statements
    EXCEPTION
    WHEN exception type
    statements to handle exception
    WHEN another exception type
    statements to handle exception
    END





    Counting (record how many times something has happened, we use count)

    e.g.
    count <-- count + 1
    count <-- count - 1