473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Diffrence between ++i and i++

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.
Nov 14 '05 #1
12 5605
lr******@yahoo.com (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?
Nov 14 '05 #2
Mac
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

Nov 14 '05 #3
lr******@yahoo.com (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 (ir*******@freenet.de)
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
Nov 14 '05 #4
Predecrement vs. postdecrement ... the ++ in front of something increments
and then evaluates ... the ++ after something evaluates and then increments.

Ed
"Luai" <lr******@yahoo.com> wrote in message
news:64**************************@posting.google.c 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.

Nov 14 '05 #5

"Luai" <lr******@yahoo.com> 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.
Nov 14 '05 #6
*** post for FREE via your newsreader at post.newsfeed.com ***

Ben Pfaff <bl*@cs.stanford.edu> wrote in
news:87************@blp.benpfaff.org:
lr******@yahoo.com (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! =-----

Nov 14 '05 #7
Andrew Clark <an*****@syr.edu> writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in
news:87************@blp.benpfaff.org:
lr******@yahoo.com (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.
Nov 14 '05 #8
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.

Nov 14 '05 #9
*** post for FREE via your newsreader at post.newsfeed.com ***

Ben Pfaff <bl*@cs.stanford.edu> wrote in
news:87************@blp.benpfaff.org:
Andrew Clark <an*****@syr.edu> writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in
news:87************@blp.benpfaff.org:
lr******@yahoo.com (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! =-----

Nov 14 '05 #10
lr******@yahoo.com (Luai) wrote in message news:<64**************************@posting.google. com>...
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.


A proper understanding of the pre and post ++ and -- operators is
vital if you're going to be programming in C. I don't know if that
particular question should have been worth 18 points, though.

The expression i++ evaluates to the current value of i; as a *side
affect*, i is incremented some time before the next sequence point.

The expression ++i evaluates to the current value of i *plus 1*; as a
*side affect*, i is incremented sometime before the next sequence
point.

The -- operator works the same way, just replace "plus" with "minus"
and "increment" with "decrement".

In both cases above, you don't care about what the expression
evaluates to, just the side effect (incrementing i by 1), so either
expression works just as well. OTOH, if the autoincrement/decrement
expression is part of a larger expression, one or the other may be
called for. For example, given a variable i initialized to 1, the
expressions

j = i++ * 2 /* i == 1, j = 1 * 2 */

and

j = ++i * 2 /* i == 1, j = 2 * 2 */

will assign different results for j, so whether you use pre- or
post-increment does matter.
Nov 14 '05 #11
In article <87************@blp.benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.edu> wrote:
Andrew Clark <an*****@syr.edu> writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in
news:87************@blp.benpfaff.org:
lr******@yahoo.com (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.


Maybe the exam question was:

"Write a full expression which increases i by one, without using a
postincrement operator"...
Nov 14 '05 #12
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
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.

Didn't you mean "++C Programmers..."?

--
Mabden
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rubbersoul | last post by:
I have a table with 3 colums: Date (datetime) End (datetime) Sys_id (INT) What I need to do is write a query that will do a calculation and return the diffrence in hours between date and...
7
by: ranjeet.gupta | last post by:
Dear All, What is the diffrence between the below two notation; char bytes; char bytes; both are one bytes, It may be odd to ask question like above, But does it matter if i allocate like...
3
by: | last post by:
is there any performance diffrence between MC++ 2003 ,other .NET languages and MC++ 2005 (cause of an optimization) ???
5
by: dost | last post by:
can u tell me the diffrence between...... private public and protected .........in........these.......which one...is access modifier....and which one..is specifier....... and....the diffrence...
8
by: Vijay | last post by:
Can you tell me what is the diffrence between structure and union.
1
by: jitender164 | last post by:
hello all pls help me to explore the diffrence b/w c and c++ by using a program example code that can illustrate the diffrence between the structure programing and object oriented programing....
3
by: raju123 | last post by:
what is diffrence between c# and vb.net
0
by: MALIK771 | last post by:
write now i am using SQL7 with .net 2002. on XP. and SQL server with .net 2002 on server2000 but i don't have idea about MYSQL. could u tell me the diffrence between them.
12
by: trysenthil | last post by:
hi all., i want to know the diffrence between the decode and case function that is available in oracle....help me ya...
0
parshupooja
by: parshupooja | last post by:
Hey All, I have a 5 textboxes on my asp.net C# page First texbox has 06:15 AM value second textbox has 09:15AM value. third texbox has 03:30 PM fourth has 5.30 PM I want to calculate...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.