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
...
*/
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.
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'
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.
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 ;
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.
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.
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.
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.
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.
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.
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.
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.