Why?
i = 7 * 7;
print i;```
There's your code. Actions applied to integer data are quantized before unary operators act upon them. You increment i twice, and the state of precedence of pre(in|de)crementing operators forces them to be processed first.
EDIT: if you were to do ```markupi = i++ * ++i;``` you'd notice that the answer comes to 37. This is because ++i and i++ are two different operations. ++i has higher precedence than i++. The former means "increment before", and the latter: "increment after", which is why we use it in for-loops.
EDIT 2: Just so you see all the variations for i = 5:
```markupi = i * ++i; // 36
i = i++ * ++i; //37
i = i++ * i++; //27
i = i++ * i; //26
i = ++i * ++i; //49```
I'm actually not very sure. I tested it and pre-increment is adding two to the original value which makes i = (5+2)^2 instead of i = (5+1)^2. Not sure why it does that. I put it in a loop and tested it too.
#include <iostream>
using namespace std;
int main()
{
for (int i = 0, j = 0; i <= 25; i++, j++)
{
cout << "iteration:\t" << j << endl
<< "i:\t\t" << i << endl
<< "i++:\t\t" << i++ << endl
<< "++i:\t\t" << ++i << endl
<< "------------------" << endl;
}
return (0);
}
You will find that i++ shows the value before the incrementation, but i shows the value after incrementation (as does ++i).
Not sure. You should try googling: "preincrement operator c++" and tell us what you find. Or you could take your question to stackoverflow.
I would look around but it's 5:43 AM and I have to be up at 10:45.
stealth- wrote: Huh, interesting. Good question/answer :) no it isnt. it was unhelpful before the edits and is misleading after them. its as simple as the 1st edit: ++i increments befor any other action is taken and i++ does it after BUT no we do not use it in for loops cus of that. whether you do for(i=0;i<10;i++) or for(i=0;i<10;++i) will have no difference on teh loop because these for loops are just a more compact way of writing
while(i<10)
{
<code to execute>
i++; //or ++i
}```
as yuo see the placemnt of ++ has no inpact on the loop
come on people this is basic shit. learn your unary operatorz and loops
nice to list all the possibiltis for those who still dont get it though
just a panda wrote: [quote]stealth- wrote: Huh, interesting. Good question/answer :) no it isnt. it was unhelpful before the edits and is misleading after them. its as simple as the 1st edit: ++i increments befor any other action is taken and i++ does it after BUT no we do not use it in for loops cus of that. whether you do for(i=0;i<10;i++) or for(i=0;i<10;++i) will have no difference on teh loop because these for loops are just a more compact way of writing
while(i<10)
{
<code to execute>
i++; //or ++i
}```
as yuo see the placemnt of ++ has no inpact on the loop
come on people this is basic shit. learn your unary operatorz and loops
nice to list all the possibiltis for those who still dont get it though[/quote]
You stated verbatim what I added to my post. The "EDIT:"'s are misleading. They were done 30 seconds after the original post to expand on the original answer.
And actually, you're not totally correct about why for-loop post|pre-incrementation is arbitrary.
++i increments i and evaluates to the new value of i.
i++ evaluates to the old value of i, and increments i.
The reason this doesn't matter in a for loop is that the flow of control works roughly like this:
>test the condition
>if it is false, terminate
>if it is true, execute the body
>execute the incrementation step
Because (1) and (4) are decoupled in this simple example, either pre- or post-increment can be used, but don't make the mistake of thinking they're equal.
EDIT: but still, calm your shit. OP is clearly a beginner. Not everyone here knows these things, and if you wanna split hairs, be prepared for me to split it further.
Arabian wrote: You stated verbatim what I added to my post. its as simple as the 1st edit: well wouldya lookit that. i actually gave you credit for the part you got right i was saying your unnecessary addendum was misleading and i implied that while you like to sound as if you know your stuff, you dont. i now make that statement as clearly as possible so there you have it.
And actually, you're not totally correct about why for-loop post|pre-incrementation is arbitrary.
++i increments i and evaluates to the new value of i. i++ evaluates to the old value of i, and increments i. hey look! its exactly what weve already established!
>test the condition >if it is false, terminate >if it is true, execute the body >execute the incrementation step
Because (1) and (4) are decoupled in this simple example, either pre- or post-increment can be used, but don't make the mistake of thinking they're equal. and hey again! it is exactly the same as what i wrote! wow! who wouldve thought?
EDIT: but still, calm your shit. OP is clearly a beginner. Not everyone here knows these things, and if you wanna split hairs, be prepared for me to split it further. you havent split shit man. youve just shown that you dont understand the difference between effectively the same and thus not the reason why wed use one in favour of the other in the scenario of a for loop which your edit misleadingly suggested. and i didnt do it for op. i did it cus you need to stop trying to sound smart about basic shit that youre still lacking a complete grasp on. when a respectable member ends up thinking your post is interesting and helpful when it couldve been more concise, more easily understandable and correct, thats a sad day for hbh
ghostraider100 wrote: int i=5; i=++i*++i; printf("%d",i);
output: 49
What is the reason behind it? Can any one explain it. Assuming your code is in C, take a look at http://c-faq.com/expr/seqpoints.html. It's undefined behaviour.
Also, just to show that you won't always get 49, here's a Javascript example:
5
js> i = ++i * ++i;
42```
just a panda wrote: [quote]Arabian wrote:
++i increments i and evaluates to the new value of i. i++ evaluates to the old value of i, and increments i.
and hey again! it is exactly the same as what i wrote! wow! who wouldve thought?
EDIT: but still, calm your shit. OP is clearly a beginner. Not everyone here knows these things, and if you wanna split hairs, be prepared for me to split it further. you havent split shit man. youve just shown that you dont understand the difference between effectively the same and thus not the reason why wed use one in favour of the other in the scenario of a for loop which your edit misleadingly suggested. and i didnt do it for op. i did it cus you need to stop trying to sound smart about basic shit that youre still lacking a complete grasp on. when a respectable member ends up thinking your post is interesting and helpful when it couldve been more concise, more easily understandable and correct, thats a sad day for hbh[/quote]
I pointed out the difference in flow and reiterated the difference in operator effect because YOU are not completely correct by declaring triviality for all increments in that type of simple loop for all languages. As starofale pointed out, not all languages operate in the same way, and interpreted or lazy evaluations, explicit left to right evaluation and different compilers will produce different results. It is decidedly not arbitrary.
I admit the for loop statement was a bit off, and I don't always audit my posts before editing.
But seriously chill the fuck out. It's not the end of the world. The "sad day" bit is very melodramatic. If you don't like it, post more. I can't be the only operational member here and not be expected to fuck up every once in a while.
i didnt do it for op
Good to know you're only posting because you felt the need to whip your e-dick out "correct" someone about the only thing you seem "know". Have you tried doing something constructive? Like killing yourself?
Arabian wrote: I pointed out the difference in flow and reiterated the difference in operator effect because YOU are not completely correct by declaring triviality for all increments in that type of simple loop for all languages. As starofale pointed out, not all languages operate in the same way, and interpreted or lazy evaluations, explicit left to right evaluation and different compilers will produce different results. It is decidedly not arbitrary. dont try to use starofales post to validate what you wrote. if anything it does the opposite since then none of your examples can be said to be correct as they are officially undefined
I admit the for loop statement was a bit off, and I don't always audit my posts before editing. :)
But seriously chill the fuck out. It's not the end of the world. The "sad day" bit is very melodramatic. If you don't like it, post more. I can't be the only operational member here and not be expected to fuck up every once in a while.
[quote] i didnt do it for op
Good to know you're only posting because you felt the need to whip your e-dick out "correct" someone about the only thing you seem "know". Have you tried doing something constructive? Like killing yourself?[/quote] its not about anyones activity, inactivity or my "e-dick." its about yours. what im saying is that if you want to really be of help you need to act with the amount of knowledg you actually have and not stroke your e-dick by posting shit like "Actions applied to integer data are quantized before unary operators act upon them" which to anyone unfamiliar enough with the language to not know whats going on will be absolutely useless. in fact, its pretty useless for answering the question no matter who asks it. had you just posted the first bit of your first edit and the examples, that wouldve been a great reply somebody has to tell you that youve got to stop trying to sound smarter then you are if your intention is to help. and if your uncertain about something dont fuckin post. otherwise be concise, to the point and clear. you know, like you were about me killing myself. thats style.
anyhow this has been fun. hope we cant do it again sometime.
oh and starofale. good post. there is (or was havent checked up on it) a similar behavior in php which was reported as a bug. though to be fair in higher level languages odd behavior might simply be a side effect of how memory management works. tata
just a panda wrote: posting shit like "Actions applied to integer data are quantized before unary operators act upon them"
as to tell you that youve got to stop trying to sound smarter then you are if your intention is to help
Very specific terms used so that OP will have a chance to look up for himself what they mean. If we attempt to save OP's brain from the proper terms he'll never have a chance to see how they fit into the grand scheme of things. Just like we say ternary, binary, and monadic.
And please, tell me how smart I am. Please do.
none your examples can be said to be correct as they are officially undefined
*for alternate languages, yes, and neither could yours, and that was the point. I'm not speaking in maxims about the flow of standard loops here. You are. The point was you were not completely in the right and shouldn't be indicting other members with such fervor and clear sense of superiority.
simply be a side effect of how memory management works
… i'll let that one speak for itself.
Arabian wrote: Very specific terms used so that OP will have a chance to look up for himself what they mean. If we attempt to save OP's brain from the proper terms he'll never have a chance to see how they fit into the grand scheme of things. Just like we say ternary, binary, and monadic.
Yeah its true, but still I'm unable to guess what is the meaning for that.
ghostraider100 wrote: [quote]Arabian wrote: Very specific terms used so that OP will have a chance to look up for himself what they mean. If we attempt to save OP's brain from the proper terms he'll never have a chance to see how they fit into the grand scheme of things. Just like we say ternary, binary, and monadic.
Yeah its true, but still I'm unable to guess what is the meaning for that.[/quote]
Arabian wrote: Have you tried doing something constructive? Like killing yourself?
What the fuck, man. That shit isn't cool at all. I have much respect for this place but didn't you say you wanted to be a mod in the shoutbox awhile back? I don't think we should have mods who can't keep their cool…without spouting off strange insults such as telling some dude to kill himself…
It's the internet and all, but he's a real person…that's just not cool.