---
# System prepended metadata

title: Pseudocode Standard
tags: [cop3014c, recitation]

---

![Recitation Review Logo](https://hackmd.io/_uploads/S1hwoZ9i6.png)

Pseudocode Standard <small>(<i>Revised: </i> January 21, 2025)</small>
===

Pseudocode serves as a bridge between the initial problem analysis phase and the structured code of a programming language. This pseudocode convention establishes a clear and consistent framework for outlining algorithms and program logic in a human-readable format. Adherence to these guidelines promotes code that is well-organized, understandable, and easier to translate into specific programming languages. The conventions provided here cover essential elements like data types, control structures, file operations, functions, and common data structures, empowering precise and effective algorithm design.

**Purpose:** To provide a structured, human-readable way to outline algorithms and program logic, independent of specific programming language syntax.

---

**Table of Contents**

[TOC]

---

General Guidelines
--

* **Focus on Readability:**  Pseudocode should be easy to understand, even for someone unfamiliar with the exact programming language you'll use later.
* **Utilize Plain Language:** Use common English-like phrases and descriptive variable names.  Avoid programming-language-specific keywords when possible.
* **Indentation:** Indent nested code blocks to signify relationships and control structures. 

Specific Conventions
--

* **Capitalization:** Capitalize initial words of major statements (e.g.,  `START`, `IF`, `WHILE`, `READ`, `DISPLAY`).
* **Variable Naming:** Use meaningful variable names (e.g., `studentName` instead of `x`).
* **Line Separation:** Write one distinct statement per line.
* **Input/Output:** Use standard terms like:
    * `READ`, `INPUT`, or `GET` for input 
    * `DISPLAY`, `OUTPUT`, or `PRINT` for output
* **Conditional Statements:** 
    * `IF ... THEN ... ELSE ... END_IF`  
* **Loops:** 
    * `WHILE (condition) DO... END_WHILE`
    * `DO ... WHILE(condition)`
    * `FOR ... END_FOR`
* **Assignment:** Use standard terms like:
    * `SET` or `ASSIGN`
    * Use `=` or an arrow `<-`  (e.g., `SET total = total + price`).
* **Data Types:** Include data types immediately after "Declare"
    * `DECLARE String firstName`
    * `DECLARE Integer age`
    * `DECLARE Real prices array` 

* **Files**
    * **Opening:** `OPEN file "data.txt" for reading`
    * **Reading:** `READ nextLine from file`
    * **Writing:** `WRITE message to file`
    * **Closing:** `CLOSE file`

* **Functions**

    * **Definition:**
       ```plaintext
       FUNCTION calculateArea WITH PARAMETERS  length: float,  width: float RETURNS float
           SET area = length * width
           RETURN area
       END FUNCTION
       
       FUNCTION printGreeting
           DISPLAY "Welcome to shape calculator!"
       END FUNCTION
       ```

    * **Calling:**
       ```plaintext
       CALL printGreeting()
       SET totalArea = calculateArea(10, 5) 
       ```

* **Arrays**

    * **Declaration:** `Declare integer numbers array of size 10`
    * **Accessing Elements:** `numbers[3] = 25`
    * **Iterating:**
    * 
       ```plaintext
       FOR index = 0 TO size of numbers - 1
           DISPLAY numbers[index]
       END FOR 
       ```

* **Switches**

    ```plaintext
    SWITCH (color)
       CASE "red":
           DISPLAY "Stop"
       BREAK
       CASE "yellow":
           DISPLAY "Caution"
       CASE "green":
           DISPLAY "Go"
       BREAK
       DEFAULT:
           DISPLAY "Invalid color"
    END SWITCH
    ```

* **Comments:**

    * Use comments freely to explain non-obvious sections of your pseudocode. 
    * Comment markers may vary  (e.g.,  `//` for single-line, or block comments).



Example: File Processing with Functions
--

```plaintext=
FUNCTION readFileAndCalculateAverage WITH PARAMETERS filename: string RETURNS float
    DECLARE float numbers array
    DECLARE integer count

    OPEN file filename for reading
    WHILE not end of file
        READ nextNumber from file
        SET numbers[count] = nextNumber
        SET count = count + 1
    END WHILE
    CLOSE file

    RETURN calculateAverage(numbers) 
END FUNCTION

FUNCTION calculateAverage WITH PARAMETERS  data array: float RETURNS float
    DECLARE float sum 
    DECLARE float avg

    FOR index = 0 RO size of data - 1
        SET sum = sum + data[index]
    END FOR

    SET avg = sum / size of data
    RETURN avg 
END FUNCTION

START
    DECLARE string filename
    DECLARE float average

    DISPLAY "Enter filename: "
    READ filename

    SET average = readFileAndCalculateAverage(filename)
    DISPLAY "Average: ", average 
END
```


Example: Display Greeting with Name
--

```plaintext=
FUNCTION displayGreeting WITH PARAMETERS name: string
    DISPLAY "Hello, ", name, "!"
END FUNCTION

START
    // Calling the function 
    DISPLAY "Enter your name: "
    READ inputName

    CALL displayGreeting(inputName) 
END
```

<br/>

---
<center><small style="font-size:8pt"><i>Created with Google Gemini</i><br/>&copy;2024 Vanessa Coote - All rights reseerved.</small></center>