4E. Constants

Constants

A constant is a "variable" that you cannot change. To declare a constant, write const in front of the type. For example,

  const int nine = 9;
  const int size = 200;
  const double pi = 3.1415926536;


Why use constants?

Programmers make a lot of mistakes. The more opportunities that you have to make mistakes, the more mistakes you will make.

You can reduce your mistakes by taking away opportunities to make mistakes. Constants are one way of doing that. If you do not want to change something, mark it as a constant so that you cannot accidentally change it.