• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loops with if statement  RSS feed

 
Tengen Toppa Gurren Lagann
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain me how this code work.
i'm confused about the if statement part because int i = 0 is set to zero but how can you get the remainder if int i is zero?



And also please explain to why it output like this?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tengen Toppa Gurren Lagann wrote:i'm confused about the if statement part because int i = 0 is set to zero but how can you get the remainder if int i is zero?

Because that's how the '%' operator works. You might also want to try it with negative values and see what it produces - you may be surprised.

And also please explain to why it output like this?

Not quite sure what you want here, but the simple answer is: Because it's what you told it to do.

Perhaps you could show us what you expected to see. That might help us to clear up your problem.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at a basic level, mathematical (as opposed to computer) integer division returns two whole numbers. one is how many times 'a' goes into 'b', and one is how many are left over. Or, how many times can you subtract a from b before you don't have enough to pull out any more, and then what's left over.

so, 13 divided by 4 would give you 3-remainder-1. you can pull 4 out of 13 three times, and you would have one left over.

31 divided by 7 gives you 4 remainder 3, etc.


so...

0 divided by 12 - you can pull 12 out of 0 exactly ZERO times, and you have ZERO left over.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!