0% found this document useful (0 votes)
13 views

Naming Variables - Programming Basics - KS3 Computer Science Revision - BBC Bitesize

yyyyy

Uploaded by

Mayrin Solis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Naming Variables - Programming Basics - KS3 Computer Science Revision - BBC Bitesize

yyyyy

Uploaded by

Mayrin Solis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Home News Sport Earth Reel Worklife

Home Learn Study support Careers My Bitesize More

KS3

Programming basics
Naming variables
Programming is writing computer code to create a
program, in order to solve a problem. To program a
computer, you need to know how programs are
constructed.

Part of Computer Science Programming

Save to My Bitesize

Revise Test

Pages
Arithmetic

Variables

Naming variables

Working with variables

Input and output


Naming variables
Each variable 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
camelCase 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 name Comment

l A poor choice – it has no meaning

length Okay but a bit vague

side_length Good

sideLength Good

side length Wrong – don’t use spaces

Example

This Python (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
data .

The data types 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.

In many programming languages variables must be declared 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.
Previous page Next page

Variables Working with variables

More guides on this topic

Introduction to programming Selection in programming

Iteration in programming Boolean logic Arrays and lists

Procedures and functions Writing error-free code

Related links

Jobs that use Computer Science BBC Click BBC Technology news

Tech Tent Headsqueeze Khan Academy Raspberry Pi

Language:
Code Club Subscription Educational App Store Subscription
English

Home Earth Travel Music Sounds

News Reel Culture TV

Sport Worklife Future Weather

Terms of Use About the BBC Privacy Policy Cookies Accessibility Help Parental Guidance
Contact the BBC Get Personalised Newsletters Why you can trust the BBC Advertise with us

Do not share or sell my info

© 2024 BBC. The BBC is not responsible for the content of external sites. Read about our approach to external
linking.

You might also like