Activity 2-1 - Variables and Assignments



"All variables must be declared before they are used in a C++ program."

All variables used in a C++ program must be declared.  The declaration of a variable can be done in several different ways; 1) right at the beginning of the main function, 2) right before its use in the program, and 3) right before the main function.  As it was shown in the sample C++ program in the previous lab, we declared variables of type integer and float right at the beginning of the main function.  In general, the syntax for variable declaration is:
Type_name  Variable_Name_1, Variable_Name_2;

Example:
int  number_of_cases, bottles_per_case;

Here is the list of the most common variable types used in C++:
 

Variable Name
Variable Name
short (short int) - 2 bytes 
short x; or short x = 34;
long - 4 bytes
long x; or long x = 34;
int - 4 bytes
int x; or int x = 34;
float - 4 bytes, 7 digits precision
float x; or float x = 34.56;
double -  8 bytes, 15 digits precision
double x; or double x = 34.56;
long double - 10 bytes, 19 digits precision 
char - holds any single character on the keyboard
char c; or char c = 'a';
bool - True (1), False (0)
bool x; or bool x = TRUE;

Every variable is identified by a name that is referred to as identifier.  An identifier must start with either a letter or the underscore symbol.  The remaining characters must all be letters, digits, or the underscore symbol.

In general, every variable should have a type.  An attempt to assign a value of a different type other than the original assigned type to a variable is referred to as "type mismatch".  One has to be very careful that not all compilers will allow type mismatch, i.e., may produce an error when such attempts are made.

Arithmetic Operators and Expressions
To perform mathematical calculations, we combine variables and/or numbers using arithmetic operators such as: + for addition, - for subtraction, * for multiplication, / for division, and % to find the remainder of a divisionThe arithmetic operators can be used with numbers of type int, long, double, float, long double, short, or with a combination of different types. When you use arithmetic operators, you need to be careful that if the correct variable types are not used, the result can be very different from what you were expecting.  For example: 7.0/2 = 3.5, 7/2.0 = 3.5, however, 7/2 = 3 (not 3.5). The reason for such a significant difference is because the last one is actually using integer division.  Perhaps the most significant problem will be seen in cases where the numerator is smaller than the denominator.  In such cases the integer division will result in 0.  An example of such a case is 5/9 = 0.

There are some shorthand notations that combine the assignment operator ( = ) and an arithmetic operator. The general form for this notation is:
Variable Operator = Expression, which is equivalent to:  Variable = Variable Operator (Expression).

The expression can be another variable, a constant, or a more complicated arithmetic expression.  Here are some examples:
 
 

Shorthand Notation
Equivalent
total += 2;  total = total+2;
amount +tax - rebate amount amount + (tax - rebate)

Similarly, we may use -=, *=, /=, and %=.

Exercise 21-1
Determine the answer to the following statements:
    Question 2.1     3 -= 4*5;
    Question 2.2     40 %= 16/2;
    Question 2.3     88 /= 82%4;