Variable Declaration & Assignment

Computer Science: OCR GCSE, topic 2.2.1

Variable Declaration and Assignment

A variable must be declared before a value can be assigned.

Declaration

Declaration

  • Before variables can be used, they must be declared.
  • Declaring a variable means telling the computer:
  • What the identifier (name) should be.
  • What type of data will be stored.
Assignment

Assignment

  • Changing the value stored inside a variable is called assignment.
  • The value assigned to a variable must match the data type of the variable.
Language specifics

Language specifics

  • Some languages don’t need you to declare variables before you use them:
  • These languages automatically declare the variable before the first assignment.

Test yourself on Variable Declaration & Assignment

  • All languages require variables to be explicitly declared. True or false?

  • What is important to check when assigning a value to a variable?

Practise this topic for free