+ 2
Why we use i++,+i and --i
23 Respostas
+ 4
Mahmud Nabil In fact I am not sure about it. It was just told me by someone i trust😬
He told me something like this:
Moreover, modern CPUs are highly complex beasts, which can execute a lot of operations in parallel, and quite often, "simple" operations like these can be entirely hidden by running them in parallel with larger, more complex ones.
I found this on:
https://stackoverflow.com/questions/6889647/is-there-any-performance-difference-with-i-vs-i-1-in-c
+ 3
+ 3
I didn't say anything else, I posted a link with a correct description of these operators.
+ 3
Alexander Thiem I've doubts about the time efficiency thing you mentioned. Whould you provide some references?
+ 3
If i=1 and we use increment operator ++ then by this
cout<<i++ ;
The snswer will be 1 however after this statement the value of i will be 2
And its opposite in case of ++i in it cout<<++i;
The answer will be 2 because increment operator is used first and then i is written.
+ 3
For Increment and decrement
+ 3
for example
int x=5;
cout <<++x; //6
int x=5;
cout <<x++; //5
+ 2
$¢𝐎₹𝔭!𝐨𝓝 I understand what you mean but in your answers was not explained the difference to i=i+1 (i think time efficience)
+ 2
This was not part of the question.
+ 2
Mahmud Nabil I wrote this thing with time efficency without being sure it is right..... Does not matter anymore
+ 2
Oh tnx uzair
+ 2
Consider a=5,b=10
++a means pre increment.... value of "a" increment first before any "opperation" carried out so a*b=6*10=60,then a=6 and b=10
a++ means post increment
.....The value of "a" increment after opperation carried out. So a*b=5*10..then a vaue increment then a=6 ....same is a-- and --a ....but hear value is decrement
+ 1
Alexander Thiem I just wanted to know the explanation because it's unintuitive to me. Sorry for whatever made you use '😬' emoji.
+ 1
We use these for increment or decrement....Meanwhile
Let's take a variable V=0.
Now,
If you want an increment of a number or variable.....
Then,
V=V++;
What is simply does is it will increase V's value by one after the operation or u can say after the compilation of the program.
Now if u want to decrement of a number or variable.
Then,
V=--V
Now, what this does is first it will first decrease the value of V then print the value of it.....suppose V's value is 2 --V will decrease the value of V by 1 then the result will be V=1......!
I hope u got my point...!☺
+ 1
for example:
int i=1;
i++; // i=1
cout<<i; // 2
int j=1;
++j; // j=2
cout<<j; // 2
0
I think that :
cout<<i++;
Takes the same time as
cout<<i;
or atleast less time than
cout<<i;
i=i+1;
so it firtsly saves time and secondly shortens your code.
0
Alexander Thiem
If person know how the thing works then s/he definately know why and where to use.
0
This Is Things In Your Code
For Loop Or Anything
In for loops
You things to break your loop. If You Dont make your loop is infinity loops
0
Np 😊