Show
Programming basicsProgramming 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. 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. There are some rules about variable names: Consider these example variable names, all of which could be variable names to store the length of a side of a square:
ExampleThis Python (3.x) program uses two meaningful names when calculating the perimeter of a square: >>> side_length = 5 >>> perimeter = side_length * 4 >>> print(perimeter) 20Because meaningful names have been used in this code, it is easy to know what each variable is used for. Data typesVariables 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:
In many programming languages variables must be declared before they can be used, for example:
In some languages, such as Python, you can simply start using the variable without declaring it.
KS3 Subjects
Why is it important that variable names are meaningful?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.
What is a meaningful variable name?You can define symbolic variables with meaningful names. Meaningful variable names, like PAY_RAISE, describe the contents of the variable and make CLISTs easy to read and maintain. Note that an ampersand (&) is not part of a variable name; it tells the CLIST to use the value of the variable.
What makes a good variable name?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.
Why is it important to follow a consistent naming convention for variables?Regardless of how you choose to name your variables, always ensure that your naming conventions are consistent throughout the code. Consistency allows others to more easily understand your code.
|