answersLogoWhite

0


Best Answer

Variables are not constant. If they were constant they would be constant. For example, time is a variable in many graphs. Were it to be constant it would mean that time stopped.

In programming such as C++ you often make a constant variable. The term variable refers to a usable instance of a type such as an integer. Making it constant means that it is unchangable but you can still use in the same manner as a variable. It is good practice to use constant variables rather than writing the values directly in the code because if the constant value changes it only needs to be changed in one place. For example:

without constant variable:

if (screenWidth > 200) {

lineEnd = 200;

}

drawLine(0, lineEnd);

with constant variable:

const int MAX_SCREEN_WIDTH = 200;

if (screenWidth > MAX_SCREEN_WIDTH) {

lineEnd = MAX_SCREEN_WIDTH;

}

drawLine(0, lineEnd);

In practice the value may be spread over many files and a search for 200 may find many other results that may be the wrong value.

Constant variables also help indicate the intention of the code without needing a comment. In the above example, the 200 does not indicate that it is the maximum screen width.

Making the variable constant means that the compiler will show an error if you try to change it.

MAX_SCREEN_WIDTH = 400; // will cause error

It is common practice to use uppercase letters to indicate a constant variable.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why does variables need to be constant?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Are constant variables and derived variables the same?

Constant variables are constant, they do not change. Derived variables are not constant. They are determined by the other values in the equation.


Are constant variables and independent variables the same?

No they are not the same. A constant variable keeps going at a constant rate.


How do you get the constant in algebraic expression?

By itself you cannot. You need to have a set of values for the variables and the expression which you need to solve.


How do you keep control variables constant?

Dependent upon the variables, you need to take into consideration factors that can affect the outcome of the result; what will make the result vary in any way. If this, for example, entails the variable to be kept constant time, you will monitor the time and repeat it throughout the experiment. This is my understanding of constant variables; hope this helped.


What is constant variables?

Variables are items, which change their values during the execution of a program. Constants do not change the value during the execution of a program.


What is the constant and the variables in Boyles law?

The constant k is a...constant specific for the system considered.


What is a monomial with no variables?

constant


How many variables are kept constant in a experiment?

All variables except one, the experimental variable, are kept constant in an experiment.


What are the two types of variables?

The two types of variables are the CONSTANT and CONTROL.


What are all the variables?

There are 'constant variables' , 'independant variables' and 'dependent variables' Constant Variable- things in the experimment that should be kept the same Independant variables- something that can be varied in an experiment Dependant variable- something that can be affected


What does constant mean in math?

Constant is a number with no variables. For an example, 12a, but this is a constant 12.


What are constant variables and y r they important in an experiment?

. Constant variables are variables which cannot be changed with the experiment. To remember their name is easy just think about Constance. They are important to an experiment because without all three variables there would be no complete experiment. Also the constant variables are important to an experiment because they help complete the result. Without a constant variable you. Would not be testing correctly