• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack help  RSS feed

 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In the event that you enter a 0, it parses it, then pushes it onto the stack, and then ends the loop. I can't figure out why it's pushing it onto the stack if one of the loop parameters is met? (j != 0)
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will only stop when the guard is false. In this case, that means that newStack.isEmpty() or j == 0. Since you add things to the stack, it will never again be empty, and you will keep pushing data onto the stack.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob - the two conditions are being ANDed not ORed, so the loop will stop when j is 0

@Alex - loop conditions are only checked at the start of each iteration thru the loop. So, on line 6 you set the value of j to 0, on line 7 you push it onto the stack and only then is the loop condition checked again. If you don't want the 0 to be put on the stack, then you need to change it to
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see you back, Joanne.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:@Rob - the two conditions are being ANDed not ORed

I know. My OR is from De Morgan: !(A && B) == !A || !B for any A and B.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!