programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# For loops with if statement

Tengen Toppa Gurren Lagann
Greenhorn
Posts: 1
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
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
• 1
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.