Paul Clapham wrote:Or you can do what I do. Never use the value returned by either x++ or x--. Just use them as standalone expressions to increment a variable.
Of course if you plan to write certification exams you're sure to find this sort of question, so you'll have to know the difference between the two. But once you've passed the exams, don't ever use them again. Generations of programmers who have to maintain your code will thank you.
fred rosenberger wrote:Not to beat a dead horse, but by your logic, modulus would never return anything but zero. 12 divided by five would be 2.4, so 0 is the remainder.
15 divided by 8 is 1.875, so zero is the remainder...
Modus, by definition, is the leftover after integer division. so 2 goes into 1 exactly zero times, with one left over. just like 5 goes into 12 two times, with two left over...
Liutauras Vilda wrote:I think Dave gave an info, that the division is based on how many whole numbers fit. 3 % 2 as in this case, how many 2's fit to 3, so you divide 3 / 2, you get 1 and remainder 1, so you are correct on that one. And now if you get back to your initial example 1 % 2, how many 2's fit to 1? 0. You don't take fractions.
Liutauras Vilda wrote:
But you are not dividing, you use modulus operation in actual question.
What would be 3 % 2 according to you?
Dave Tolls wrote:0.5 is not a whole number, so 1 divided by 2 is 0 remainder 1.
So the modulus is 1.