Variables and constants

<< Previous: Data types Next: Selection >>

Assignment is described at the end of the video after the data types section

Variables vs constants

Variables and constants are both storage locations for data that might be used in a program. They differ because if the storage location stores data that will not change throughout the running of the program it is known as a constant. Names of constants are generally in capital letters to dientify them as constants.

Naming conventions

As well as using capital letters for constants, a number of other rules must be followed when naming storage locations. These include:
- Use meaningful names
- Must begin with a letter or underscore
- Should use camel case or snake case

Meaningful names means they should give you some idea what data will be stored in the location. Camel case is a method that can be used when a variable needs more than one word to be meaningful. With camel case first words start with lowercase and each new word starts with a capital e.g. camelCase. Snake case uses all lower case and separates words with underscores e.g. snake_case. It doesn't matter which you use, but picking one and sticking to it can make it easier to recall your variable names as you type new code.

Knowledge check


Questions:
Correct:

Question text


<< Previous: Data types Next: Selection >>

© All materials created by and copyright S.Goff