English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

an example would be
const double PI=3.14;
for(float x=PI;whatever;x++){
cout< }

that was just an example of using constants;

but you could do the same without using constants;
double PI=3.14;
for(int x=PI;whatever;x++){
//This would also work
}


so what use do we have of using constants for somethings instread of variables

2007-12-05 08:30:48 · 4 answers · asked by amun25 2 in Computers & Internet Programming & Design

4 answers

Constants don't change, variables can change.

You can't mistakingly change a constant. For example like this:

if(pi=3.00){
//do something
}

If pi is a constant, you'll get an error message. If pi is a variable, you won't.

2007-12-05 08:33:20 · answer #1 · answered by krohn211 2 · 1 0

It's just so that no one else changes them. It also indicates to other programmers that the variable is never intended to change.

2007-12-05 08:34:12 · answer #2 · answered by Mavis 5 · 1 0

Variables use more memory. So if you have a large program you need to consider this.

2007-12-05 08:36:32 · answer #3 · answered by Slick 5 · 0 0

Its just validation, it eliminates problems caused by changing variables.

It prevents you from accidentally changing its value and messing everything up. Its very professional. I must admit, I dont use them but I should do.

2007-12-05 08:36:21 · answer #4 · answered by sirantonycartwright 3 · 1 0

fedest.com, questions and answers