# Calculations

• Print
This chapter is from the book

## Using int Variables and Constants

A variable allows the program to perform its calculation outside of the cout statement.

```  1: #include <iostream>
2:
3: using namespace std;
4:
5: int main(int argc, char* argv[])
6: {
*7:  const int Dividend = 6;
*8:  const int Divisor = 2;
*9:
*10:  int Result = (Dividend/Divisor);
*11:  Result = Result + 3;// Result is now its old value+3=6
*12:
*13:  cout << Result << endl;
14:
15:  // Note: You must type something before the Enter key
16:  char StopCharacter;
17:  cout << endl << "Press a key and \"Enter\": ";
18:  cin >> StopCharacter;
19:
20:  return 0;
21: }```

Lines 7–13 have been changed.

Lines 7 and 8 declare variables named Dividend and Divisor and set their values to 6 and 3, respectively. The = is called the assignment operator and puts the value on the right-hand side into the variable on the left-hand side. These variables are declared as type int, which is a number with no decimal places.

Although these are technically variables, because they have names, the use of the word const on these variable declarations makes it clear to the compiler that the program is not allowed to change the content of these variables in any way (unlike, for instance, the variable StopCharacter, which stores whatever character the user types in). Declarations with const are often called constants (mathematically inclined readers will recall that pi is the name of the constant whose value is 3.14159).

Line 10 declares a variable and assigns the result of part of an expression to the variable. It uses the names of the constants on lines 7 and 8 in the expression, so the value in Result depends on the content of those constants.

Line 11 is perhaps the hardest one for non-programmers. Remember that the variable is a named location in memory and that its content can change over time. Line 11 says, "Add the current content of Result and the number 3 together and put the calculated value into the location named by Result, wiping out what used to be there."

The output of this example is still 6. This shows that you can change the implementation of a design (that is, the code you have written to accomplish a task), and still produce the same result. Therefore, it is possible to alter a program to make it more readable or maintainable.