Unsigned int sum = 0;


#1

In Employee.m, // Sum up the resale value of the assets unsigned int sum = 0; for (Asset *a in assets) { sum += [a resaleValue]; } return sum; }

May I know why is the sum be 0 at first and not be unsigned int sum; ?
I would appreciate if one could address my concern. Thank you :slight_smile:


#2

When declaring variables, always initialising them is considered good programming practice; otherwise you would be relying on compiler’s default initialisation policy: that is, what value does the compiler assign to a variable left uninitialised?

Left uninitialised:

[code]unsigned int sum;
// What is the value of sum here? either zero or some garbage.

// Increment by foo
sum += foo;
[/code]
Initialised:

[code]unsigned int sum = 0;
// What is the value of sum here? definitely, zero.

// Increment by foo
sum += foo;
[/code]
[Become a competent programmer faster than you can imagine: pretty-function.org]