Difference between Num++ and ++ Num?

I want output to be
/* Outputs
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
*/
but right now it only give me Number:1 forever.
I tried num=++num, it works but not with num++,why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 int num = 1;
while (num < 6) {
  
  num = num + +; 
  cout << "Number: " << num << endl;
}

/* Outputs
Number: 1
Number: 1
Number: 1
Number: 1
Number: 1
... 
*/
closed account (48T7M4Gy)
http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i

The effective result of using either is identical. In other words, the loop will do the same exact thing in both instances.

In terms of efficiency, there could be a penalty involved with choosing i++ over ++i. In terms of the language spec, using the post-increment operator should create an extra copy of the value on which the operator is acting. This could be a source of extra operations.
Last edited on
closed account (z05DSL3A)
The difference between ++x and x++ it to do with incrementing before or after evaluating x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    int x{ 0 }, 
        y{ 0 };

    while (x < 6) 
    {
        std::cout << "x: " << ++x << "\ty: " << y++ << std::endl;
    }

    return 0;
}
x: 1    y: 0
x: 2    y: 1
x: 3    y: 2
x: 4    y: 3
x: 5    y: 4
x: 6    y: 5
Press any key to continue . . .
Last edited on
> right now it only give me Number:1 forever.

num = num++; engenders undefined behaviour.

If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined. http://en.cppreference.com/w/cpp/language/eval_order
Undefined behavior: Renders the entire program meaningless if certain rules of the language are violated.
http://en.cppreference.com/w/cpp/language/ub


To get the output that you expect:
1
2
3
4
5
int num = 0 ;
while (num < 6) {

  std::cout << "Number: " << num++ << '\n' ;
}

Last edited on
closed account (48T7M4Gy)
Undefined behavior

1) Between the previous and next sequence point a scalar object must have its stored value modified at most once by the evaluation of an expression, otherwise the behavior is undefined.

i = ++i + i++; // undefined behavior
i = i++ + 1; // undefined behavior
i = ++i + 1; // undefined behavior (well-defined in C++11)
++ ++i; // undefined behavior (well-defined in C++11)
f(++i, ++i); // undefined behavior
f(i = -1, i = -1); // undefined behavior

... notwithstanding the ambiguity of 'undefined' vs 'well-defined'
Last edited on
closed account (48bpfSEw)
I like the answer of Steve Jessop:

(...) I'm in the habit of typing ++it because:

1) Some day it might make a difference, for some iterator type, and I don't want to have to do something special for that type.

2) Personally I think the prefix operator more clearly expresses the intent: "increment it", as opposed to "use it and then increment".
(...)


http://stackoverflow.com/questions/1077026/incrementing-iterators-it-more-efficient-than-it




above refers to iterators.
Last edited on
closed account (48T7M4Gy)
The point is what oporto-girl makes ie num = num + + is undefined and so is num = ++num. Just pre or post incrementation difference on it's own is a trivial difference according to the references, and this is backed up by the variety seen in teaching and texts.
Last edited on
> num = num++ is undefined and so is num = ++num

num = ++num ; does not engender undefined behaviour.

The standard defines prefix increment and decrement of scalar objects in terms of compound assignment.
num = num += 1 ; has well-defined behaviour, and so does num = ++num ;
Last edited on
closed account (48T7M4Gy)
Yeah oporto-girl is probably right but the cppreference is actually ambiguous. Something Cubbi might look at seeing he is the guru.

Realistically though there is no substantial reason for writing code like that anyway, so it is an esoteric case or simply an error due to inexperience/oversight not requiring major reasons for a compiler to protect against.
How expressions are evaluated is fundamental to even a rudimentary understanding of C++.

There is absolutely nothing ambiguous in what is stated about prefix/postfix increment/decrement and assignment/compund-assignment in cppreference.
closed account (48T7M4Gy)
How expressions are evaluated is fundamental to even a rudimentary understanding of C++.

There is absolutely nothing ambiguous in what is stated about prefix/postfix increment/decrement and assignment/compund-assignment in cppreference.


Of course there is there wouldn't have been any mention of it if there wasn't. I'd rather hear what Cubbi says, he is the guru as we all know. A simple good faith statement has been made by me and it is not an arguing point with someone who only thinks he knows what he's talking about. Go ahead now oporto-girl and red mark this one too you creepy troll.
closed account (48T7M4Gy)
MYOB - what's the game now? A bunch of nerds like you going to gang up on me because they kowtow to a creepy troll like oporto-girl who has been trolling and insulting me for months. It's time you behaved yourself too cyber-bully too weak to stand up for yourself.
Last edited on
closed account (48T7M4Gy)
I understand your interest TSG. I have found you to be a respectful participant here and I have no quarrel with you, but oporto-girl is a name specifically targetted at the individual because the internet is a big place and this person has left a trail which my advisers have tracked.

oporto has relentlessly given me a hard time here for a considerable amount of time going back many months, and for no reason at all.

I have never sought oporto's commentary on my contributions and never interfered in oporto-girls. I have not demeaned anybody unless they have started their drivel on me. The person I refer to as oporto-girl suffers for some obscure reason perhaps because I present an intellectual threat which that person renders as a jealousy-trolling-inadequacy-response, something they demonstrate time after time by their snide remarks and attacks. Perhaps oporto is a king of the kids type cum pedo, I can only guess. oporto-girl is kow-towed to by a couple of weak individuals here who have pilloried a number of other people, even recently. The stacks on the mill attacks on me have already started just a short while ago and I expect them to continue.

A long answer TSG but there you go :)
Last edited on
closed account (48T7M4Gy)
See what I mean, they're at it again with their report wuss.

The gang's out in force. Stacks on the mill. The same crew who pilloried closed account the other day who did absolutely nothing wrong except upset one dope focussed on the guys post count - another disgraceful worm backed up by a pompous bird-named worm. Closed_account showed more nouse and helped more beginners than the lot of these ppl put together.

They lurk here thinking they are the gift to programming. Just a bunch of nerd freaks spoiling the fun for decent and interested people willing to share.

http://www.cplusplus.com/forum/lounge/195433/ lists the creeps in full. This site should ban them because it's the reason virtually nobody comes here any more.
Last edited on
You're not getting reported just because you're attacking JLBorges. You're getting reported because you're creating drama where there is none, and worse still in doing so you're hijacking a legitimate thread. If you have nothing helpful to add regarding OP's question, go away.
Last edited on
closed account (48T7M4Gy)
Another one crawls out of the woodwork folks. If you really are an Argentinian you remind me of the Belgrano. A whole pile of bubbles coming up from the depths of the ocean.

'go away' really? Is that your best shot?
Last edited on
It wasn't a shot, it was friendly advice. But since apparently you've decided to behave like an asshat, I'm not going to acknowledge you further.
Thank you for everyone's help. I have fixed the issue and learned more.
Thank you all so much!
:)
closed account (48T7M4Gy)
Friendly advice? 'Asshat, (donkey fetish) 'go away', protecting a bully , my oh my you're a real charmer. All froth and bubble from the bottom of the sea.
Topic archived. No new replies allowed.