Variable which is constant
A constant is a value that cannot be altered by the program during normal execution, i. This is contrasted with a variable , which is an identifier with a value that can be changed during normal execution, i.
Thus, as its name implies — the value is constant. Thus, as its name implies — the value can vary. A literal constant is a value you type into your program wherever it is needed. Examples include the constants used for initializing a variable and constants used in lines of code:. In addition to literal constants, most textbooks refer to symbolic constants or named constants as a constant represented by a name.
The face value of constants is known. The value of the variables is unknown. From the above table now you have a clear picture of what is the difference in constant and variable. Now let us solve some examples which will make it more clear what is variable and constant. Example 1 :. Solution :. Add 4 to each side.
So, the value of x is 4. Example 2 :. Divide each side by 3. So, the value of x is 9. Example 3 :. The key difference when comparing a constant to a variable is that the value associated with a variable name may change during program execution.
For example 'highScore' would need to be variable to change throughout a game. The content and organisation of a computer's memory is not fixed - so neither is the value that is pointed at by a variable.
When data is read from a variable, the content of the memory location is copied and used in calculations. Constants and variables In a program, data values can be constant or variable.
Constants Data values that stay the same every time a program is executed are known as constants. Examples of a constant within a game might be: the unit of gravity the number of lives available for the player the amount of time allowed for a level in a game Variables Variables are data values that can change when the user is asked a question, for example, their age.
0コメント