Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Introduction to Loop Structures
There are 3 types of loop structures that are used within pseudocode, which are pre-condition, post-condition and count controlled loops.
This type of loop is likely the one that most are familiar with, where the loop is controlled by a condition set before the loop. It follows the structure of WHILE, ENDWHILE.
WHILE <condition>
<code to be executed>
ENDWHILE
It may not execute at all, depending on the set condition. E.g:
WHILE num1 < 5
num1 <- num1 + 1
ENDWHILE
The expression will continue evaluating until the condition becomes FALSE, at which point the loop terminates.
This type of loop depends on a condition set after the code within it has executed once, as the condition is evaluated after the code. It follows the structure of REPEAT, UNTIL.
REPEAT
<code to be executed>
UNTIL <condition>
E.g:
REPEAT
num1 <- num1 + 1
UNTIL num1 = 10
This type of loop depends on a counter set within its initial statement. It follows the structure of FOR, TO, NEXT.
FOR <integer variable> TO <desired limit>
<code to be executed>
NEXT <integer variable that was used>
The value of the variable used as a counter can be assigned within the FOR statement.
FOR x <- 1 TO 10
OUTPUT x
NEXT x
The default increment value for the count variable is 1, however this can be changed with the addition of an extra keyword, STEP. A negative increment can be used, and as soon as the variable reaches a value equal to or above the limit, the loop terminates.
FOR <variable> TO <limit> STEP <increment>
<code to be executed>
NEXT <variable>
Introduction to Declaring Variables and Arrays in Pseudonaja
To use variables in Pseudonaja we first need to declare the variable. This can be done using the following command:
DECLARE <variable name> : <data type>
An example of this is:
DECLARE num1 : INTEGER
DECLARE num1, num2, num3 : INTEGER
Available data types:
INTEGER
A single integer number
REAL
A single float / real number
BOOLEAN
A binary TRUE or FALSE value
CHAR
A single character
STRING
A string of multiple characters
ARRAY
An array of fixed length of a particular data type
To assign a value to a variable, instead of an = sign, an <- is used instead, after the variable has been declared.
num1 <- 3
In pseudocode you declare arrays slightly differently:
DECLARE <array name> : ARRAY [<lower bound>:<upper bound>] OF <data type>
An example of this is:
DECLARE myArray : ARRAY[0:100] OF INTEGER
Unlike lists in python arrays can only consist of a single data type as well as a fixed length.
Two dimensional arrays are declared as follows:
DECLARE <array name> ARRAY [<1ower bound 1>:<upper bound 1>, <1ower bound 1>:<upper bound 2>] OF <data type>
An example of this is:
DECLARE my2DArray ARRAY [0:100, 0:1] OF CHAR
To access a 1D array:
DECLARE myArray : ARRAY[0:100] OF INTEGER
myArray[19] <- 29
To access a 2D array:
DECLARE my2DArray ARRAY [0:100, 0:1] OF CHAR
my2DArray[12, 1] <- 'B'
Where the first value within the assignment statement is the row to be accessed, and the second value is the column to be accessed.
Introduction to Inputting and Outputting Data
To input values, the INPUT command is used, however the variable that is being input into must first be declared, as follows:
DECLARE <variable name> : <data type>
INPUT <variable name>
An example of this would be:
DECLARE num1 : INTEGER
INPUT num1
To output values, the OUTPUT command is used, as follows:
OUTPUT <value>
Multiple values can be output at the same time by using commas between each value.
OUTPUT <value1>, <value2>, <value3>
An example of a single output would be:
OUTPUT num1
Or multiple outputs:
OUTPUT num1, num2, num3
Introduction to Conditional Structures
In pseudocode, there are two types of conditional structures that are utilised, being IF statements and CASE statements.
IF statements utilise a specific set of keywords that need to be included, being IF, THEN, ENDIF.
IF <condition> THEN
<code to be executed>
ENDIF
E.g:
IF num1 = 3 THEN
OUTPUT "num1 is 3"
ENDIF
IF statements can also include an ELSE clause to be able to branch into other code depending on if the IF statement has passed or not.
IF <condition> THEN
<code to be executed>
ELSE
<code to otherwise be executed>
ENDIF
E.g:
IF num1 = 3 THEN
OUTPUT "num1 is 3"
ELSE
OUTPUT "num1 isn't 3"
ENDIF
Nested IF statements can also be used, however, care must be taken with inserting ENDIFs into the correct locations. An example of this would be:
IF num1 = 3 THEN
IF num2 = 3 THEN
OUTPUT "num1 and num2 are 3"
ELSE
OUTPUT "Only num1 is 3"
ENDIF
ELSE
IF num2 = 3 THEN
OUTPUT "Only num2 is 3"
ELSE
OUTPUT "Neither num1 or num2 is 3"
ENDIF
ENDIF
CASE statements are where multiple branches of code can be executed, depending on the value of the variable given to the statement. They follow the keywords CASE OF, ENDCASE.
CASE OF <variable>
<value1> : <code to be executed>
<value2> : <code to be executed>
etc...
ENDCASE
E.g:
CASE OF num1
1 : OUTPUT "num1 is 1"
2 : OUTPUT "num1 is 2"
3 : OUTPUT "num1 is 3"
ENDCASE
An OTHERWISE clause can be added, which acts similarly to an ELSE clause. It must always be the last case.
CASE OF <variable>
<value1> : <code to be executed>
<value2> : <code to be executed>
etc...
OTHERWISE : <code to be executed>
ENDCASE
E.g:
CASE OF num1
1 : OUTPUT "num1 is 1"
2 : OUTPUT "num1 is 2"
3 : OUTPUT "num1 is 3"
OTHERWISE : OUTPUT "num1 is not 1, 2 or 3"
ENDCASE
Introduction to Functions and Procedures
Functions are algorithms that can be called to execute an already written set of code, and return a value to where they were called. They use the keywords FUNCTION, RETURNS, ENDFUNCTION. Functions can be used either with or without parameters.
Alternatively, with parameters:
The keyword RETURN is used to specify a value to be returned within the function. Upon encountering a RETURN statement, it is executed immediately, and any lines after the statement are ignored.
Procedures are similar to functions, however they do not return any value. They use the keywords PROCEDURE, ENDPROCEDURE. Procedures can also be used with or without parameters.
With parameters:
Procedures must be called using the CALL keyword, used respectively as follows:
An example of procedure definition and calling:
FUNCTION <function name> RETURNS <data type to return>
<code to be executed>
ENDFUNCTION
FUNCTION <function name>(<parameter1> : <data type>,
<parameter2> : <data type>...)
RETURNS <data type to return>
<code to be executed>
ENDFUNCTION
FUNCTION Add(num1 : INTEGER, num2 : INTEGER) RETURNS INTEGER
num3 <- num1 + num2
RETURN num3
ENDFUNCTION
PROCEDURE <procedure name>
<code to be executed>
ENDPROCEDURE
PROCEDURE <procedure name>(<parameter1> : <data type>,
<parameter2> : <data type>...)
<code to be executed>
ENDPROCEDURE
CALL <procedure name>
CALL <procedure name>(<parameter1>, <parameter2>, ...)
PROCEDURE Add(num1 : INTEGER, num2 : INTEGER)
num3 <- num1 + num2
OUTPUT num3
ENDPROCEDURE
CALL Add(10, 5)
Introduction to Records
A record is a composite data type that contains multiple different data types. It contains multiple data items that can be accessed via dot notation. They are defined with keywords TYPE, ENDTYPE, and contain declarations:
TYPE <data type name>
DECLARE <item1> : <type>
DECLARE <item2> : <type>
...
ENDTYPE
Then, variables can be declared of the data type that has been defined, to be able to access elements within. To access items within the data type, the name of the variable is used, followed by a dot, then the name of the item to be accessed.
TYPE Person
DECLARE FirstName : STRING
DECLARE LastName : STRING
DECLARE FavFood : STRING
DECLARE Age : INTEGER
ENDTYPE
DECLARE Friend1 : Person
Friend1.FirstName <- "Jack"
Friend1.LastName <- "Pollier"
Friend1.FavFood <- "Lasagna"
Friend1.Age <- 17
⌘/⌃ F
Show / Hide find an replace menu
⌘/⌃ O
Load a local file into the editor
⌘/⌃ S
Save current editor content to a local file
How to help update Pseudonaja
Ready to contribute? Here’s how to set up Pseudonaja for local development.
First you must fork the repo relevant to what you plan to change. Then you must clone the repo to your local machine in order to make changes.
$ git clone git@github.com:PutYourUsernameHere/editor.git
$ git clone git@github.com:PutYourUsernameHere/docs.git
$ git checkout -b name-of-your-bugfix-or-feature
Now make your changes.
While there are no automated tests set up, it is highly encouraged that you thoroughly test anything your changes may have effected.
If your changes effect the codemirror files, you will need to roll up the editor files to a single file for use in the browser. The repo is already set up with a rollup configuration, so simply run:
$ npx rollup -c
Then just run the server:
$ node index.js
Once you are sure your changes work and haven't broken anything, push them to your forked repo.
$ git add .
$ git commit -m "Your detailed description of your changes."
$ git push origin name-of-your-bugfix-or-feature