Privacy and Security Notice

Archived Messages for C_CLASS98@cebaf.gov: Student Question

Student Question

Dr. C. Allen Butler (allen@va.wagner.com)
Sat, 07 Mar 1998 09:49:29 -0800 (PST)

On Wed, 4 Mar 1998, Student X wrote:

> Hi,
>
> I programmed Chapter 2 # 19. It doesn't seem to make any difference
> whether the while loop has
>
> i++ or ++i.
>
> Is it really supposed to, or is it compiler dependent?
>
> - Student X
>

The value of the EXPRESSION i++ is the current value of i. The value
of the EXPRESSION ++i is one more than the current value of i. In
both cases, the value of the VARIABLE i is incremented by one.
However, if the value of the expression is not used, then effectively
there is no difference.

For Example:

Different: x = i++; x = ++i;
Different: while (i++ < 0) {stuff} while (++i < 0) {stuff}
No Difference: for (i=0; i<10; ++i) {stuff} for (i=0; i<10; i++) {stuff}

Dr. C. Allen Butler (757)-727-7700 Voice
Daniel H. Wagner Assoc. (757)-722-0249 FAX
2 Eaton St., Ste. 500 allen@va.wagner.com
Hampton, VA 23669