Variables

Last Updated on

Variables are a very easy topic to understand with pseudocode. I cannot stress how easy using, assigning, and comparing variables are. It is really easy to grasp this, so we will be going over how-to assigning variables, how to use them (for conditionals, recursive functions with parameters and more), how to override variables in pseudocode and finally how to some basics about them!

We follow the pseudocode standard set by the main Computer Science exam board in the UK, AQA, allowing a universal set of functions and operators to be used. This makes it much easier for programmers to understand each other’s code without comments!

Code Icon

Basics:

A Guide to Variables in Pseudocode

Simple Variable Declaration

In pseudocode, it is very easy to declare variables! We just need to mention the variable name then assign a value to it. For example, if we were making a calculator app, we would make a variable called input. We could then use this later to perform our calculations, which we will get into later!

        input // input variable ready for assignment

For constants it is very similar, however when declaring them, we must make sure to put their name in capital letters. For example if we were storing the user’s input as a constant to then perform calculations on, we would say INPUT and then assign our value to it. This is to distinguish both variables and constants from each other in pseudocode, which helps wen working in teams of multiple developers!

        INPUT // input constant ready for assignment

Assigning a value to a variable

In pseudocode, when assigning values to a variable or constant, it is best practice to use the assignment arrow (←). In our online pseudocode editor, we have buttons below the editor that allow you to copy the assignment arrow, and other operators that cannot be found on a default keyboard, to paste and use in your pseudocode.

        OUTPUT "Enter a number"
        input ← USERINPUT  // user input assigned to variable

So, say we have just declared the variable input, we will want to assign a value to that. So, if we were taking the user input, we would use input ← USERINPUT, as seen in the example above. This assigns the user’s input from an output or prompt field and then allow us to use that data. We can also just assign regular data to it, for example a string. ExampleString ← "pseudoeditor.com is awesome", would be a valid assignment.

Using variables in Pseudocode

It is really easy to access the data within a variable. Say we were performing some calculations on an integer that the user entered, we would use the variable name then the operation we will be performing. We would use the input variable we assigned a value to earlier and say, input * 5, if it was an integer.

        OUTPUT "Enter a number"
        input * 5  // user input multiplied by 5

It can just be a simple thing like outputting the variable back to the user, in that case we would just use the output feature and say, OUTPUT input, although that looks silly!

        OUTPUT "Enter a number"
        input ← USERINPUT  // user input assigned to variable
        OUTPUT input  // outputs user's input from a variable
Code Icon

That is it! You have successfully declared, assigned a value to, and then used variables and constants in pseudocode. It was easy wasn’t it? Now, lets move you onto a more advanced guide! You can read more of our guides and then get even better at pseudocode, without even leaving our site!