Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide and must not be used by students during a NEA where internet access is not allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Variables and Constants

Variables and constants let your program remember and use data (e.g. names or scores)

Both variables and constants have a name and a value

The value is the actual data your program needs to remember (e.g. the number 3.141)

The name lets your program call that value something more helpful (e.g. PI)

The difference between a constant and a variable

The value of a variable can changed when the program runs.

The value of a constant is set once before the program runs, then can't be changed whilst the program runs.

Advantages of using variables

Variables let your program react and respond to data rather than always doing exactly the same thing every time they run.

Advantages of using constants

Constants make your programs easier to read and less prone to errors because the names are easier to understand than just the values.

Top tips

  • Make sure your choose sensible names for your variables that describe the data that they store.
    high_score is a much better name than myvariable1 because it makes your code easier to understand for you and other programmers.
  • Variable and constant names can't contain spaces.
    Calling a variable high score will cause an error message. Replace spaces with an underscore (snake_case) or make the next letter a capital letter (CamelCase)
  • Be consistent with the naming convention that you use (e.g. always use CamelCase or snake_case, don't mix and match)
    snake_case is recommended for Python and CamelCase is recommended for VB.
    These naming conventions (sets of rules for how to name your variables / constants) wont break your code if you don't follow them but they help you to be more consistent so your code is less likely to have errors.
Integer variables
   

Integers are whole numbers

Variables let you store data

Integer variables let you store whole numbers

In this example, a variable called score is declared then set to the integer value of 100

INTEGER score SET score TO 100
Dim score As Integer score = 100
score = 100
score = 100
score is integer set score = 100
Real variables
   

Reals are numbers that can contain a decimal place. 

Reals are also known as floats or floating point numbers because they may have a decimal point floating somewhere inside them.

Variables let you store data.

A real variable lets you store a number that can contain a decimal place.

In this example, a variable called height is declared then set to the real value of 6.2

REAL height SET height TO 6.2
Dim height As Double height = 6.2
height = 6.2
height = 6.2
{ real data types not included in Eduqas pseudocode reference so they *shouldn't* come up in an exam } height is real set height = 6.2
Boolean variables
   

Boolean means one of two possible values: usually True or False.

Variables let you store data.

Boolean variables let you store the either value True or False, but nothing else

In this example, a variable called hungry is declared then set to True

BOOLEAN hungry SET hungry TO True
Dim hungry As Boolean hungry = True
hungry = True
hungry = true
hungry is boolean set hungry = TRUE
Character variables
   

Characters are single letters or digits such as '1', 'H' or '?'

Variables let you store data.

Character variables let you store a single letter, number or punctuation mark.

In this example, a variable called letter is declared then set to a full stop.

CHARACTER letter SET letter TO '.'
Dim letter As Char letter = '.'
letter = '.'
letter = "."
letter is character set letter = "."
Integer constants
   

Integers are whole numbers (wiith no decimal places).

Constants store a value that is set once and then never changes.

Integer constants let you give a whole number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called NUMBER_OF_TRIES is set to the integer value of 10

CONST INTEGER NUMBER_OF_TRIES SET NUMBER_OF_TRIES TO 10
Const NUMBER_OF_TRIES As Integer = 10
NUMBER_OF_TRIES = 10
NUMBER_OF_TRIES = 10
{ no mention of constants in eduqas pseudocode reference } NUMBER_OF_TRIES is integer set NUMBER_OF_TRIES = 10
Real constants
   

Reals are numbers that can contain a decimal place.

Constants store a value that is set once and then never changes.

Real constants let you give a decimal number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called PI is set to the real value of 3.141

 

CONST REAL PI SET PI TO 3.141
Const PI As Double = 3.141
PI = 3.141
PI = 3.141
{ no mention of constants or reals in the pseudocode spec } PI is real set PI = 3.141
Boolean constants
   

Boolean means one of two values (usually either True or False).

Constants store a value that is set once and then never changes.

Boolean constants let you give a boolean value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called CHEAT_MODE is set to the boolean value of True

CONST BOOLEAN CHEAT_MODE SET CHEAT_MODE TO True
Const CHEAT_MODE As Boolean = True
CHEAT_MODE = True
CHEAT_MODE = false
{ no mention of constants in pseudocode spec } CHEAT_MODE is boolean set CHEAT_MODE = FALSE
Character constants
   

Characters are single letters or digits such as '1', 'H' or '?'

Constants store a value that is set once and then never changes.

Character constants let you give a character value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called KEY_QUIT is set to the character value of 'Q'

CONST CHARACTER KEY_QUIT SET KEY_QUIT TO 'Q'
Const KEY_QUIT As Char KEY_QUIT = 'Q'
KEY_QUIT = 'Q'
KEY_QUIT = "Q"
{ no mention of constants in pseudocode reference } KEY_QUIT is character set KEY_QUIT = "Q"
String variables
    

A string is some text such as "Hello" or "Area 51"

Variables let you store data.

String variables let you store text.

In this example a variable called name is set to the string value of "Bob"

STRING name SET name TO "Bob"
Dim name As String name = "Bob"
name = "Bob"
name = "Bob"
name is string set name = "Bob"
String constants
    

A string is some text such as "Hello" or "Area 51"

Constants store a value that is set once and then never changes.

String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.

In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"

CONST STRING ERROR_MESSAGE SET ERROR_MESSAGE TO "Don't panic!"
Const ERROR_MESSAGE As String = "Don't panic!"
ERROR_MESSAGE = "Don't panic!"
ERROR_MESSAGE = "Don't panic!"
{ no mention of constants in pseudocode reference } ERROR_MESSAGE is string set ERROR_MESSAGE = "Don't panic!"
Casting (converting) to a string
     

Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.

This example converts the integer number 1000 to a string containing "1000"

INTEGER int_number = 1000 ' There's not explicit casting. ' It's done implicitly by the data types of the variables STRING str_number = int_number
Dim IntNumber as Integer IntNumber = 1000 Dim StrNumber as String StrNumber = IntNumber.toString()
int_number = 1000 str_number = str(1000)
int_number = 1000 str_number = str(1000)
{ not specified in eduqas pseudocode reference }
Casting (converting) to an integer
     

Often it's useful to round a decimal number (real data type) to a whole number (integer data type).

It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer

This example converts the string "1000" to the integer 1000

STRING str_number = "1000" ' There's not explicit casting. ' It's done implicitly by the data types of the variables INTEGER int_number = str_number
Dim StrNumber as String StrNumber = "1000" Dim IntNumber as Integer IntNumber = Convert.toInt32(StrNumber)
str_number = "1000" int_number = int(1000)
str_number = "1000" int_number = int(1000)
{ not specified in eduqas pseudocode reference }
Casting (converting) to a real
     

Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)

This example converts the string "1.1234" to the real value 1.1234

STRING str_number = "1.1234" ' There's not explicit casting. ' It's done implicitly by the data types of the variables REAL real_number = str_number
Dim StrNumber as String StrNumber = "1.1234" Dim DblNumber as Double DblNumber = Convert.toDouble(StrNumber)
str_number = "1.1234" float_number = float(str_number)
str_number = "1.1234" float_number = float(str_number)
{ not specified in eduqas pseudocode reference }

Loading...