Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Diffrence between ++i and i++

Reply
Thread Tools

Diffrence between ++i and i++

 
 
Luai
Guest
Posts: n/a
 
      04-12-2004
I made my midterm exam in the C language course.
I lost 18 marks off 100 because I didn't relaize this killing fact:

in (for loops) there is no difference between incrementing the loop in
these two ways:

for (i=0; i < 10 ; ++i)

and

for (i=0; i < 10 ; i++)

( notice the difference is between i++ and ++i )

What are your comments on this.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      04-12-2004
(Luai) writes:

> in (for loops) there is no difference between incrementing the loop in
> these two ways:
>
> for (i=0; i < 10 ; ++i)
>
> and
>
> for (i=0; i < 10 ; i++)


Why and how did you think they might be different?
 
Reply With Quote
 
 
 
 
Mac
Guest
Posts: n/a
 
      04-12-2004
On Sun, 11 Apr 2004 20:26:04 +0000, Luai wrote:

> I made my midterm exam in the C language course.
> I lost 18 marks off 100 because I didn't relaize this killing fact:
>
> in (for loops) there is no difference between incrementing the loop in
> these two ways:
>
> for (i=0; i < 10 ; ++i)
>
> and
>
> for (i=0; i < 10 ; i++)
>
> ( notice the difference is between i++ and ++i )
>
> What are your comments on this.


Sounds about right. Usually the meaning of ++i and i++ is one of the first
things you learn in c. So, if you got all the way to the midterm without
learning this, it is not a good sign. Now you know you need to study a
little more before the final if you want a good grade (mark).

HTH

--Mac

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      04-12-2004
(Luai) wrote:
>I made my midterm exam in the C language course.
>I lost 18 marks off 100 because I didn't relaize this killing fact:
>
>in (for loops) there is no difference between incrementing the loop in
>these two ways:
>
>for (i=0; i < 10 ; ++i)


which is equivalent to:

i = 0;
while ( i < 10 )
{
++i;
}

>and
>
>for (i=0; i < 10 ; i++)


which is equivalent to:

i = 0;
while ( i < 10 )
{
i++;
}

>What are your comments on this.


Since in both cases the iteration statement is evaluated only
for its side effect (increment i), while its value is discarded,
there's effectively no difference.

HTH
Regards
--
Irrwahn Grausewitz ()
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 
Reply With Quote
 
Edward E. Hopkins
Guest
Posts: n/a
 
      04-12-2004
Predecrement vs. postdecrement ... the ++ in front of something increments
and then evaluates ... the ++ after something evaluates and then increments.

Ed


"Luai" <> wrote in message
news: om...
> I made my midterm exam in the C language course.
> I lost 18 marks off 100 because I didn't relaize this killing fact:
>
> in (for loops) there is no difference between incrementing the loop in
> these two ways:
>
> for (i=0; i < 10 ; ++i)
>
> and
>
> for (i=0; i < 10 ; i++)
>
> ( notice the difference is between i++ and ++i )
>
> What are your comments on this.



 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      04-12-2004

"Luai" <> wrote in message
>
> I made my midterm exam in the C language course.
> I lost 18 marks off 100 because I didn't relaize this killing fact:
>

A penalty of 18% for not realising that ++i and i++, in some situations,
have exactly the same effect sounds rather harsh. However at least you know
now.


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      04-13-2004
Luai wrote:

> I made my midterm exam in the C language course.
> I lost 18 marks off 100 because I didn't realize this killing fact:
>
> in (for loops) there is no difference
> between incrementing the loop in these two ways:
>
> for (i = 0; i < 10; ++i)
>
> and
>
> for (i = 0; i < 10; i++)
>
> (notice the difference is between i++ and ++i)
>
> What are your comments on this.


There is no difference.
C++ programmers prefer ++i only as a "good habit"
because both the pre increment and post increment operator++
may be overloaded for a class for very large objects
where i++ returns a copy of the original object
but ++i merely returns a reference
after "incrementing" the original object.
If you are going to write C++ programs as well as C programs,
you probably should use ++i wherever you have a choice.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      04-13-2004
Andrew Clark <> writes:

> Ben Pfaff <> wrote in
> news::
>
>> (Luai) writes:
>>
>>> in (for loops) there is no difference between incrementing the loop in
>>> these two ways:
>>>
>>> for (i=0; i < 10 ; ++i)
>>>
>>> and
>>>
>>> for (i=0; i < 10 ; i++)

>>
>> Why and how did you think they might be different?

>
> I recall an exam where I was marked off for writing one of these (I
> forget which one), and to correct it the instructor wrote the other one.


You'll have to be more specific. When ++i or i++ is a full
expression, they are equivalent. When one of them is a
subexpression of a larger expression, they may not be
equivalent. So if your instructor took off points in the former
case, he (or she) was simply wrong, but in the latter case he may
have been justified.

By the way, here is the definition of a "full expression", from
C99 6.8:

4 A full expression is an expression that is not part of another
expression or of a declarator. Each of the following is a
full expression: an initializer; the expression in an
expression statement; the controlling expression of a
selection statement (if or switch); the controlling
expression of a while or do statement; each of the
(optional) expressions of a for statement; the (optional)
expression in a return statement. The end of a full
expression is a sequence point.

--
Go not to Usenet for counsel, for they will say both no and yes.
 
Reply With Quote
 
Andrew Clark
Guest
Posts: n/a
 
      04-13-2004
*** post for FREE via your newsreader at post.newsfeed.com ***

Ben Pfaff <> wrote in
news::

> (Luai) writes:
>
>> in (for loops) there is no difference between incrementing the loop in
>> these two ways:
>>
>> for (i=0; i < 10 ; ++i)
>>
>> and
>>
>> for (i=0; i < 10 ; i++)

>
> Why and how did you think they might be different?


I recall an exam where I was marked off for writing one of these (I
forget which one), and to correct it the instructor wrote the other one.

Andrew


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

 
Reply With Quote
 
Andrew Clark
Guest
Posts: n/a
 
      04-13-2004
*** post for FREE via your newsreader at post.newsfeed.com ***

Ben Pfaff <> wrote in
news::

> Andrew Clark <> writes:
>
>> Ben Pfaff <> wrote in
>> news::
>>
>>> (Luai) writes:
>>>
>>>> in (for loops) there is no difference between incrementing the loop
>>>> in these two ways:
>>>>
>>>> for (i=0; i < 10 ; ++i)
>>>>
>>>> and
>>>>
>>>> for (i=0; i < 10 ; i++)
>>>
>>> Why and how did you think they might be different?

>>
>> I recall an exam where I was marked off for writing one of these (I
>> forget which one), and to correct it the instructor wrote the other
>> one.

>
> You'll have to be more specific. When ++i or i++ is a full
> expression, they are equivalent. When one of them is a
> subexpression of a larger expression, they may not be
> equivalent. So if your instructor took off points in the former
> case, he (or she) was simply wrong, but in the latter case he may
> have been justified.
>
> By the way, here is the definition of a "full expression", from
> C99 6.8:
>
> 4 A full expression is an expression that is not part of another
> expression or of a declarator. Each of the following is a
> full expression: an initializer; the expression in an
> expression statement; the controlling expression of a
> selection statement (if or switch); the controlling
> expression of a while or do statement; each of the
> (optional) expressions of a for statement; the (optional)
> expression in a return statement. The end of a full
> expression is a sequence point.
>


It was the former. If I can find my exam booklet I'll post the problem
and solution, but IIRC it was more or less:

Write an expression for iterating though an array using a for statement

I remember the problem was such that it didn't matter which kind (pre-
or post-) the increment operator was.

Andrew


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it because of diffrence between bcc32 and gcc? jigsaw C Programming 4 05-08-2006 03:30 PM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Diffrence between application server and web server ITpro Java 2 10-29-2005 11:17 AM
diffrence between signal, variable and wire, register mohammed rafi VHDL 3 05-07-2004 05:56 PM
diffrence between wire (in verilog) and signal (in vhdl) mohammed rafi VHDL 1 05-06-2004 04:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57