Which of the following is a valid reason for giving variables meaningful names in a program?

  • Home
  • News
  • Sport
  • Weather
  • iPlayer
  • Sounds
  • Bitesize
  • CBeebies
  • CBBC
  • Food
  • Home
  • News
  • Sport
  • Reel
  • Worklife
  • Travel
  • Future
  • Culture
  • TV
  • Weather
  • Sounds

  • Home
  • Learn
  • Support
  • Careers
    • My Bitesize

Programming basics

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.

  • Test

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. Page 3of5

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 nameComment
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.
Different data types can occur in a real-life database example. In a film database, string links
to title, float or real links to rating, integer links to times viewed and Boolean links to favourite.

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.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. Page3of 5

KS3 Subjects

  1. Biology
  2. Chemistry
  3. Citizenship (Wales)
  4. Computer Science
  5. English
  6. French
  7. Geography
  8. German
  9. History
  10. Humanities - Geography (Wales)
  11. Humanities - History (Wales)
  12. ICT
  13. Maths
  14. Modern Foreign Languages
  15. Music
  16. Physics
  17. Religious Studies
  18. Science
  19. Spanish

Which of the following is a valid reason for giving variables meaningful?

Which of the following is a valid reason for giving variables meaningful names in a program? A programmer will find it easier to read and understand the code since they will know what the variables represent.

What is a reason for the importance of variables in programming?

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

Why is it important to use good naming variables for each data?

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.

Which statement describes a good variable name in a program?

A good variable name should: Be clear and concise. Be written in English. A general coding practice is to write code with variable names in English, as that is the most likely common language between programmers.