while and do-while Loops
Omitting Values When Introducing Variables
When the initial value of a variable is
unimportant, the value needs not to be specified. For example, in the previous
program the variable
inputValue has been introduced in the following manner:
double num= 0;
However, that was in some sense superfluous
as a new value was subsequently assigned to this variable when the value was
obtained from standard input by a
cin statement. In such cases, if there
is no need to have a particular value assigned to a variable, the variable is
allowed to be introduced without a designated value, like this:
is said to be uninitialized in this example. It
then stays uninitialized until a value is assigned to it.
You might wonder: what is the value of a variable in the case where no value has been provided. Perhaps uninitialized variables contain no value?
In the C++ language, a variable cannot be having no value. Thus, an uninitialized variable has a certain value even when value has not been provided. The question is which value that would be?
That cannot be known. It might be a different value each time the program is started, or it might be the same value every time; or the value might depend on the computer running the program, or it might not. It might even depend on the weather forecast. It just cannot be known. It could be any value. Consequently, it is not allowed to make a program use a value of an uninitialized variable!
|It might seem strange for you at this point, but this omission of a variable's value at its introduction is a frequent source of bugs in programs. When engaging in this behavior please be extra careful in establishing that, afterwards, the variable will be assigned a value before the variable is used.|