Naming variables - Programming basics - KS3 Computer Science Revision (2024)

Table of Contents
Naming variables Data types

Naming variables

Each is named so it is clear which variable is being used at any time. It is important to use meaningful names for variables:

For example, pocketMoney = 20 means that the variable ‘pocketMoney’ is being used to store how much pocket money you have. Right now you have £20.

The name given to each variable is up to the programmer, but ideally a variable name should have meaning, ie it should reflect the value that it is holding.

Variable naming rules

There are some rules about variable names:

  • Consistency: ‘name’ is not the same as ‘Name’ or ‘NAME’.
  • Spacing: variable names should not have a space in them. Use underscores or instead, eg total_money; totalMoney).
  • Digits: variable names should not start with a digit

Consider these example variable names, all of which could be variable names to store the length of a side of a square:

Variable nameComment
lA poor choice – it has no meaning
lengthOkay but a bit vague
side_lengthGood
sideLengthGood
side lengthWrong – don’t use spaces
Variable namel
CommentA poor choice – it has no meaning
Variable namelength
CommentOkay but a bit vague
Variable nameside_length
CommentGood
Variable namesideLength
CommentGood
Variable nameside length
CommentWrong – don’t use spaces

Example

This (3.x) program uses two meaningful names when calculating the perimeter of a square:

>>> side_length = 5>>> perimeter = side_length * 4>>> print(perimeter)20

Because meaningful names have been used in this code, it is easy to know what each variable is used for.

Data types

Variables come in all shapes and sizes. Some are used to store numbers, some are used to store text and some are used for much more complicated types of .

The to know are:

  • String (or str or text). Used for a combination of any characters that appear on a keyboard, such as letters, numbers and symbols.
  • Character (or char). Used for single letters.
  • Integer (or int). Used for whole numbers.
  • Float (or Real). Used for numbers that contain decimal points, or for fractions.
  • Boolean (or bool). Used where data is restricted to True/False or yes/no options.
Naming variables - Programming basics - KS3 Computer Science Revision (1)

In many variables must be before they can be used, for example:

  • Visual Basic - dim score as int
  • Java – int score;

In some languages, such as Python, you can simply start using the variable without declaring it.

Naming variables - Programming basics - KS3 Computer Science Revision (2024)
Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6056

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.