C ++ help ?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Oct 27, 2007
17,009
5
0
Originally posted by: Crusty
Originally posted by: tuteja1986
// thingy loops :)
#include <iostream>
using namespace std;

int main() {


for (i = 0; i <= 3; i++) {

code goes here

}

return 0;
}

Read your book :! read them :! memories every single thing from

int to for to while to array to if statements to functions to classes to fstream :!

Programming not that hard :( its only people look at it in a way "OMG I am confused" ... and i am like , that thing is less confusing then chicks or in some case , you find it much easier to learn the basic of c++ then to try to sleep with a AAA chick :(

:confused:

Hahahaha I LOL'd hard.
 
Sep 29, 2004
18,656
68
91
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.


Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!

I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.

<== 8+ years in Java and C++ in everything from VxWorks to Windows.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.


Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!

I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.

<== 8+ years in Java and C++ in everything from VxWorks to Windows.

Bloat in realtime avionics and guidance control systems is very deadly.
<== 30+ years in C, Fortran, ADA, Java and C++ in everything from VxWorks to Windows.

 

chronodekar

Senior member
Nov 2, 2008
721
1
0
slugg, thanks for that. I never really knew that stuff about stacks and the like.

IHateMyJob2004, I suppose in GUI-based applications it really doesn't matter. But if you EVER get into embedded or kernel-level programming, then there is no end to amount of optimization you can do !!
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.


Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!

I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.

<== 8+ years in Java and C++ in everything from VxWorks to Windows.

Anyone who can't get their head around the prefix increment operator should not be allowed near a C++ compiler. Seriously.

Not that I disagree with your main point: people spend too much time thinking about performance too early in the process. The old saw used to run: make it work, make it work right, then make it work fast. However making it a matter of habit to use the prefix operators is a simple thing.

As for sleeping with AAA chicks... it will be a lot easier for most of us to remember to use the prefix increment operator.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: Markbnj
Originally posted by: IHateMyJob2004
Originally posted by: deveraux
Originally posted by: slugg
Originally posted by: JACKDRUID
Originally posted by: slugg

^^ ++i is faster than i++, although most compilers will find this an optimize it for you. Careful - they're NOT the same! In this case, it's fine. Notice how the conditional is now i < 3.

umm.. i thought the only difference between ++i and i++ is the order of operation... ++i would be faster because its doing 1 less loop...

i++ copies i to a new memory space, increments it, puts it on the stack, then later assigns it back to i once other operations have been popped off the stack.

++i increments i in its already existing memory space.

These two methods are the reasons behind WHY it makes a difference in the order of operation, so you're kind of right.

The satements i++ and ++i, by themselves, are logically equivalent. However, if you combine a pre/post-increment operation with an other operation, then it would make a difference due to the order the stack is pushed/popped. Example:

int i = 0, j = 0, x, y;
x = i++;
y = ++j;

^^ After running that, your results are:

x == 0
y == 1
i == 1
j == 1

So as you can see, if you're using a counter in a for loop, ++i or i++ doesn't make a logical difference, but ++i is technically faster. Not something you really have to worry about, since most compilers will optimize it for you, but just an interesting tidbit. There is no practical reason to know this, I'm just a nerd, lol. If you really want to understand how it works, look up how to implement a for loop in assembly. You'll see that the "best" way is to do the analogous equivalent to ++i.


Thanks for the explanation. Been coding for years and I never really stopped to think about i++ or ++i other than logical uses of it. But what you said makes sense and is definitely useful to know!

I love people that freak out over hte difference. If this micro-optimization matters in the real world, tell me how so. I still have yet to find a reason to care. Use i++ at all times. The simple reason is that 90% of real world programmers will only get confused by ++i. There is no difference and ++i can only waste time due to people scratching their head.

<== 8+ years in Java and C++ in everything from VxWorks to Windows.

Anyone who can't get their head around the prefix increment operator should not be allowed near a C++ compiler. Seriously.

Not that I disagree with your main point: people spend too much time thinking about performance too early in the process. The old saw used to run: make it work, make it work right, then make it work fast. However making it a matter of habit to use the prefix operators is a simple thing.

As for sleeping with AAA chicks... it will be a lot easier for most of us to remember to use the prefix increment operator.

This is why formal education in Computer Science is a must for a professional developer in my opinion(granted, there are always exceptions...). Knowing the difference between the two was pounded into my head in the first intro C++ classes I took, and then again when I got to college and started taking the algorithms/data structures classes taught in Java. It's one of the first questions I ask someone when we are doing interviews if they claim C/C++ experience on their resume.