A community for students.
Here's the question you clicked on:
 0 viewing
Zaara
 3 years ago
what is the difference between ++top and top++in data structures and algorithms in java?
Zaara
 3 years ago
what is the difference between ++top and top++in data structures and algorithms in java?

This Question is Closed

ajprincess
 3 years ago
Best ResponseYou've already chosen the best response.0Guess u r referring the one that comes in stack @Zaara. Right?

Zaara
 3 years ago
Best ResponseYou've already chosen the best response.0yes u r ryt.... im littl confused with dat.....

ajprincess
 3 years ago
Best ResponseYou've already chosen the best response.0when u initialize the top u give the value 1 which means that there is no element in the stack.If there is one element the index of top will be 0. when u insert the first element the index shoud be iterated from 1 to 0. In this case u use ++top. The iteration has to be done before the insertion. Am I clear?

Zaara
 3 years ago
Best ResponseYou've already chosen the best response.0just compare ++top and top++... i like to knw the difference...

asnaseer
 3 years ago
Best ResponseYou've already chosen the best response.1++top means "first increment top, then read its new value" top++ means "read the current value of top, then increment it" e.g. top = 1; x = ++top; will result in x=0, top=0 whereas: top = 1; x = top++; will result in x=1, top=0

hduff
 3 years ago
Best ResponseYou've already chosen the best response.1asnaseer has nailed it... Java has two increment/decrement operators: ++ and . These operators can be used both before and after the operand they are applied to. When used before they carry out prefix incrementing or decrementing. When used after the operand they carry out postfix incrementing or decrementing. The results are different in each case. For example top= 10; newTop = ++top; System.out.println(newTop); prints 11, but top = 10; newTop = top++; System.out.println(newTop); prints 10 In the second line of the first case top is incremented by 1 and then newTop is initialized with top. In the second line of the second case newTop is incremented to the value of Top and then Top is incremented. Hope this helps...
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.