943,508 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11802
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2004
0

i++ and ++i

Expand Post »
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Aug 14th, 2004
0

Re: i++ and ++i

Quote originally posted by cscgal ...
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
//Lets declare i, and set it equal to 4.
int i = 4;

//Now say their is a function nammed goFetch(int), and we wanted to pass an increment of i to it

//i = 4 before this code gets touched, the value 4 would be passed, and then i would become 5
goFetch(i++);

//i = 4 before this code gets touched, i gets incrimented before anything else (and becomes 5), and the value 5 would be passed
goFetch(++i);

So, ++i means to incriment first then give the incrimented value, and i++ means to give the original value, then incriment.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Aug 14th, 2004
0

Re: i++ and ++i

You asked about loops and if's. Here's some:

LOOPS
for (int i = 0; i < 10; i++)
- vs -
for (int i = 0; i < 10; ++i)

no effective difference.

for (int i = 0; ++i < 10; )
- vs -
for (int i = 0; i++ < 10; )

Here, the first one will loop 9 times (1..9), the second one 10 (0..9). In both, the value of i in the body of the loop will be 1-based rather than 0 based.

IF/WHILE
if (i++ < 10)
while (i++ < 10)
- vs -
if (++i < 10)
while (++i < 10)

The second set will execute one less than the first set, same as the for ().

This is 'undefined' and tends to work differently on different compilers, and differently between optimized and non-optimized builds:

int i = j++ + j++;
int i = ++j + j++;

and the like. And though this might sound silly, consider this:

  1. #define MAX(x,y) ((x > y) ? x : y)
  2.  
  3. int i = 10;
  4. int j = 11;
  5. int k = MAX(i,++j); // j gets incremented multiple times!

That's a big advantage of small inlined functions vs #defines; the lack of those side effects:

  1. inline int MAX(int x, int y) { return ((x > y) ? x : y); }
  2.  
  3. int i = 10;
  4. int j = 11;
  5. int k = MAX(i++,j); // k will be 11
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 14th, 2004
0

Re: i++ and ++i

It`s simple
  1. int i;
  2. i++; //Postfix
  3. ++i; //Prefix
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TITAN is offline Offline
3 posts
since Aug 2004
Aug 15th, 2004
0

Re: i++ and ++i

if you still dont get it just cout << and it will all become clear
Reputation Points: 13
Solved Threads: 0
Posting Whiz in Training
marceta is offline Offline
217 posts
since May 2004
Aug 15th, 2004
0

Re: i++ and ++i

hi all. .
  1. for ( int i =0; i >10; i++; )
  2. { do something }
here i will be 0 in the first time code run .. thats mean the code will be done 10 times
  1. for ( int i =0; i >10; ++1; )
  2. { ` do something }
here i will be before the program done the first time .. thats mean the code will be done 9 times
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Aug 15th, 2004
0

Re: i++ and ++i

huh? First just a nit: it should be i < 10, not i > 10, or else the loop won't execute at all. Second, the i++/++i (another nit: you said ++1) is done at the BOTTOM of the loop, after the code in the braces, so ++i and i++ won't affect the loop either way.

Or am I sniffing too many whiteboard pens again?
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 17th, 2004
0

Re: i++ and ++i

yaeh i am sorry for this mistake i really i was tired when i wrote this reply but i am soorry again ..
#include <iostream> // This line of code is necessary to include all the C++ standard functions\classes definitions

usingnamespace std; // Set our namespace to standard (don't stress on this right now)

void main() // This creates our function main()

{ // Beginning of the program

int min=0, max=0, i=0; // We initialize 3 integer (number) variables to store

// Our range. We set them equal to zero or else they

// Will be some crazy number like -858993460. We could

// Also have said:

// int min=0;

// int max=0;

// int i=0;

// They are the same thing, it is just cleaner the other way

// Now we prompt the user to input a number for the min

cout << "Input your first number to count from: ";

// Then we wait until they press <enter> and read what they typed in

cin >> min; // We store the number they type in in the variable "min"

// Now we prompt the user to input a number for the max

cout << "Input your last number to count to: ";

// Then read in the maximum number to count to and store it in the variable "max"

cin >> max; // Now we have the maximum number they want to count to store

// Now here comes the loop:

// This is called a "For Loop". You will use these a million times.

// Ok, here is what it does. It gets a starting point

// "i=min" "i" is used as a counter

// "i" now equals min, let's say we typed in 10.

// This is the same thing as saying "i=10", but we don't

// Know WHAT the number is so we hold it in a variable: min

// "i <= max" This says, keep looping until this condition is false. 

// In this case, "Keep looping until i is greater than max."

// Let's pretend that max is 15. The loop will quit when i = 16 or over

// You might be thinking, why would i = 16 or over? "i" equals "min" (let's say 10)

// Well, the next parameter passed into the "for" loop is where we say what happens to "i"

// "i++" This tells the compiler to add 1 to "i" each time through

// This is the same thing as saying : "i = i + 1", It's just shorthand

// for(start, condition for the loop, after we go through the loop each time - do this) )

// You'll also notice we don't have a ";" after the "for" loop...

// Good "i"! :) if we put a semicolon after this, the loop would never run.

for(i=min; i <= max; i++) // That is because a ";" says we are done with the line, we ARENT... We never put a ";" after anything with a "{" after it .. look at "void main()" ..

{ // We have a "{" to say everything after "{" is in the loop

cout << i << endl; // Here we print out i each time through the loop and go to the next line 

} // Everything after "}" is out of the loop, if the loop is not finished, go back to the top

// You might be wondering what this does? If so.. let's go through the loop:

// Let's stick to our previous example of min=10 and max=15

// for(i=10, 10 <= 16, 10 + 1 (only if middle condition is not met, so i still = 10) )

// {

// cout << i << endl;

// }

// That is one time through the loop, when it hits "}" .. The loop executes the 3rd parameter:

// Which happens to be "i++". Now, i = 11 right? we just added 1 to i which was 10.

// The compiler never goes back to the first parameter which was "i=min". That was just to start "i" off.

// So, after the first loop, and we add 1 to "i", the compiler goes and check the middle condition

// To see if the loop should continue.. So:

// "11 <= 16" It's still TRUE, so we go through the loop again.

// Now we print out 11, then 12 <=16, then print 12, etc...

// Until we print out "16" , after the loop ends, 16 gets 1 added to it. i = 17

// "17 <= 16" This is FALSE, so the loop quits and the code 

// Goes past the loop and past the "}". Since there is nothing else in the program, the program is over.

} // We end "main()" ending our program

// We just completed our first for loop. Pretending we typed in 10 and 15, here is the output:

// Input your first number to count from: <we type in 10 and press ENTER>

// Input your last number to count to: <we type in 15 and press ENTER>

// 10

// 11

// 12

// 13

// 14

// 15

// Press any key to continue 

read this tutorial carefully u will get it ..
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Aug 17th, 2004
0

Re: i++ and ++i

Please stop using void main.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 17th, 2004
0

Re: i++ and ++i

Quote originally posted by cscgal ...
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
i++: do the calculation or do the comparison before the i's value is increment.
++i: increment the i's current value by 1 before doing the calculation or doing the comparison.

Hence, using in for loop, we cannot see clearly the differences.
However, using in if statement, they are clearly different.

For instance, we have i = 4.
if (i++ == 4) // the result is true since the current value of i is 4. After doing the comparison, then the i's value now becomes 5.

Whereas, if (++i == 4) // the result is false since the value of i is increment first, now the value of i becomes 5, then it does the comparison.

I hope it will help you.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Switch Statement & char loop
Next Thread in C Forum Timeline: hello;





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC