// Mr. Minich // CMPSC 201 // Ch. 2 Demo Program #3 // January 30, 2000 // Purpose - to illustrate declaration, initialization, and assignment statements. #include<iostream> using namespace std; int main() { int num1 = 1; // this statement declares AND initializes num1 int num2, num3; // this statement only declares num2 & num3. While the use
// of the comma is syntactically legal, I prefer that
// student's declare separate variables with separate statements. num2 = 400; // assigning 400 to num2, not initialization!!!
// I do not consider the above statement to be an initialization // statement. Even if the textbook gives such examples where a // variable is assigned a value towards the top of a program as // being initialization. Realize that the assignment operator is being // used in this line of code and that num2 actually contained // a garbage value after it was declared but before it was assigned // the value of 400. On the other hand, num1 IS initialized at the // top of the program. The equals symbol (=) is not being used as an // assignment operator with num1. Later in our course we will more // fully understand how it is being used in that true initialization // statement. num3 = num2; // assigning value of num2 to num3 num1 = num2 + 3 * num3; // use the order of operations cout << "num1 = " << num1 << endl;
// the line above prints "num1 = 1600" cout << "num2 = " << num1 * 2 << endl;
// the line above prints "num2 = 3200" but does not // actually change the assigned value of num2 to 3200 cout << "num2 = " << num2 << endl;
// the line above prints "num2 = 400" return 0; }// end of main