Questions tagged [pre-increment]

For issues relating to defining or performing pre increment operations.

Pre-increment operators increase the value of their operand by 1 (or another specified amount), and the value of the operand in the expression is the operand's value after the increment operation.

342 questions
0
votes
2 answers

C++ Pre and Post Increment

I'm having trouble with overloading the post increment method. My pre increment is fine. I also have pre/post decrements, and they both work perfectly. The increment and decrement body should be similar. The only difference should be ++/--, but I'm…
Jake
  • 313
  • 1
  • 7
  • 16
0
votes
4 answers

I don't understand the output of this code

I'm currently learning C, and my teacher gave us some homework. We had to identify the output of some code. I don't understand how y=4. The code is as follows int main() { int w = 3, y = 3; printf("w = %i\n", --w + w--); printf("y =…
Nxt3
  • 1,970
  • 4
  • 30
  • 52
0
votes
2 answers

Pre-increment operator returns lvalue or rvalue?

Going through other questions here, I've found that pre-increment operator in C returns rvalue, not lvalue. But, on trying the code below int a=35; printf("%d %d %d %d %d",a++,a,++a,a++,++a); I expected the output 38 39 38 36 36 But I get the…
MsPillai
  • 568
  • 1
  • 4
  • 7
0
votes
3 answers

Preincrement and Postincrement

I've been trying to understand how post and pre increments work lately and I've been over thinking it too much. Does "Product" become 25 after one iteration? Product *=5++ And does "Quotient" become 5/6 after one iteration? Quotient /= ++x
0
votes
1 answer

What happen if I use pre increment and post increment in the same statement?

I've seen an interesting statement today with post-increment and pre-increment. Please consider the following program- #include int main(){ int x, z; x = 5; z = x++ - 5; // increase the value of x after the statement…
aniskhan001
  • 7,981
  • 8
  • 36
  • 57
0
votes
1 answer

task in increment , decrement , printf() , why these are evaluated in this manner in C

#include #include int main() { int a=3; //case 1 printf("%d %d %d\n",(--a),(a--),(--a));//output is 0 2 0 printf("%d\n",a);//here the final value of 'a' //end of case 1 a=3; //case 2 printf("%d\n",(++a)+(a--)-(--a));//output is…
0
votes
2 answers

Is using pre-increment in a function call in C wrong?

void reverse(char *s,int j,int i) { printf("%d\t%d\n",i,j); if(i
Coffee_lover
  • 545
  • 6
  • 15
0
votes
1 answer

Why is the iterator preincremented when using with map.begin()?

in the example at http://www.cplusplus.com/reference/map/map/begin/ // map::begin/end #include #include int main () { std::map mymap; std::map::iterator it; mymap['b'] = 100; mymap['a'] = 200; …
user494461
0
votes
7 answers

Why is there any difference between "++i" and "i++" in a for loop?

It seems to me that for(int i = 0; i < 2; i++) and for(int i = 0; i < 2; ++i) should not do the same thing. For the second example it's more logic to me that i should equals 1 since the begining of the loop.
0
votes
4 answers

Pre and post increment in a for loop

Is it more performant to do a pre-increment vs a post-increment in a for loop in java ? Sample code : for (int i=0; i<10; i++) and for (int i=0; i<10; ++i) I notice that when i do a pre-increment, the execution time is lesser that when i do the…
lokoko
  • 5,785
  • 5
  • 35
  • 68
0
votes
2 answers

Why does post increment operator not work although preincrement does work in this code?

I am really new to programming (I'm an electronics and comm. engineer) and I am not able to figure out why one program works and the other one doesn't. I'd like to get a good understanding of recursive functions before going any further with my…
umayneverknow
  • 190
  • 1
  • 3
  • 13
0
votes
1 answer

If there any difference using ++variable instead of variable++ in a for loop?

Possible Duplicate: Is there a performance difference between i++ and ++i in C++? Difference between i++ and ++i in a loop? I know that a++ return the original value of a and then add one to a, while ++a increment a by one and return a. But I…
Aikanáro
  • 867
  • 3
  • 20
  • 42
0
votes
4 answers

How a=3 and b=4?

I found an interesting Programing question : What will be the values of a,b,c,f after executing this programe ? int i=0,a=0,b=0,c=0,f=0; while(i<=5){ switch(i++){ case 1:++a; case 2:++b; break; case 3: case 4:++c;a++;b++; …
Arjun Sunil Kumar
  • 1,781
  • 3
  • 28
  • 46
0
votes
2 answers

Is using a predecrement operator on the right hand side of an assignment valid C++?

I almost never put a ++ or -- anywhere except on its own line. I know they can lead to undefined behavior and can be hell for debugging. But for verbosity purposes, I'm tempted. Is this valid code? map dict; ... int key = ...; if…
Brian
  • 2,499
  • 3
  • 24
  • 26
-1
votes
2 answers

Why is pre and post increment operator not working in recursion?

I have the following: public static void main(String[] args){ Screen.clear(); System.out.println(depth(5,0)); } public static int depth(int n, int depth){ System.out.println(depth); if(n == 0)return…
DCR
  • 14,737
  • 12
  • 52
  • 115