when the variable is defined (e.g., integer, real, character). Once defined, the type of a C++ variable
cannot be changed. • A value which can be changed by assigning a new value to the variable. The kind
of values a variable can assume depends on its type. For example, an integer variable can only take
integer values (e.g., 2, 100, -12). Listing 1.2 illustrates the uses of some simple variable. Listing 1.2 1
2 3 4 5 int main (void) { int workDays; float workHours, payRate, weeklyPay; 6 7 8 9 10 11 12 13 }
workDays = 5; workHours = 7.5; payRate = 38.55; weeklyPay = workDays * workHours * payRate; cout
<< "Weekly Pay = "; cout << weeklyPay; cout << '\n'; Annotation #include <iostream.h> 4 This line
defines an int (integer) variable called workDays, which will represent the number of working days in a
week. As a general rule, a variable is defined by specifying its type first, followed by the variable name,
followed by a semicolon. 5 This line defines three float (real) variables which, respectively, represent
the work hours per day, the hourly pay rate, and the weekly pay. As illustrated by this line, multiple
variables of the same type can be defined at once by separating them with commas. 6 This line is an
assignment statement. It assigns the value 5 to the variable workDays. Therefore, after this statement
is executed, workDays denotes the value 5. 7 This line assigns the value 7.5 to the variable workHours.
8 This line assigns the value 38.55 to the variable payRate. 9 This line calculates the weekly pay as the
product of workDays, workHours, and payRate (* is the multiplication operator). The resulting value is
stored in weeklyPay. 10-12 These lines output three items in sequence: the string "Weekly Pay = ", the
value of the variable weeklyPay, and a newline character. When run, the program will produce the
following output: Weekly Pay = 1445.625 When a variable is defined, its value is undefined until it is
actually assigned one. For example, weeklyPay has an undefined value (i.e., whatever happens to be
in the memory location which the variable denotes at the time) until line 9 is executed. The assigning of
a value to a variable for the first time is called initialization. It is important to ensure that a variable is
initialized before it is used in any computation. It is possible to define a variable and initialize it at the
same time. This is considered a good programming practice, because it pre-empts the possibility of
using the variable prior to it being initialized. Listing 1.3 is a revised version of Listing 1.2 which uses
this technique. For all intents and purposes, the two programs are equivalent. Listing 1.3 1 #include
<iostream.h> 2 int main (void) 3 { 4 int workDays = 5; 5 float workHours = 7.5; 6 float payRate = 38.55;
7 float weeklyPay = workDays * workHours * payRate; 8 cout << "Weekly Pay = "; 9 cout <<
weeklyPay; 10 cout << '\n'; 11 } ¨ Simple Input/Output The most common way in which a program
communicates with the outside world is through simple, character-oriented Input/Output (IO)
operations. C++ provides two useful operators for this purpose: >> for input and << for output. We have
already seen examples of output using <<. Listing 1.4 also illustrates the use of >> for input. Listing 1.4
1 #include <iostream.h> 2 int main (void) 3 { 4 int workDays = 5; 5 float workHours = 7.5; 6 float
payRate, weeklyPay; 7 cout << "What is the hourly pay rate? "; 8 cin >> payRate; 9 weeklyPay =
workDays * workHours * payRate; 10 cout << "Weekly Pay = "; 11 cout << weeklyPay; 12 cout << '\n';
13 } Annotation 7 This line outputs the prompt What is the hourly pay rate? to seek user input. 8 This
line reads the input value typed by the user and copies it to payRate. The input operator >> takes an
input stream as its left operand (cin is the standard C++ input stream which corresponds to data
entered via the keyboard) and a variable (to which the input data is copied) as its right operand. 9-13
The rest of the program is as before. When run, the program will produce the following output (user
input appears in bold): What is the hourly pay rate? 33.55 Weekly Pay = 1258.125 Both << and >>
return their left operand as their result, enabling multiple input or multiple output operations to be
combined into one statement. This is illustrated by Listing 1.5 which now allows the input of both the
daily work hours and the hourly pay rate. Listing 1.5 1 #include <iostream.h> 2 int main (void) 3 { 4 int
workDays = 5; 5 float workHours, payRate, weeklyPay; 6 cout << "What are the work hours and the
hourly pay rate? "; 7 cin >> workHours >> payRate; 8 weeklyPay = workDays * workHours * payRate; 9
cout << "Weekly Pay = " << weeklyPay << '\n'; 10 } Annotation 7 This line reads two input values typed
by the user and copies them to workHours and payRate, respectively. The two values should be
separated by white space (i.e., one or more space or tab characters). This statement is equivalent to:
(cin >> workHours) >> payRate; Because the result of >> is its left operand, (cin >> workHours)