• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

% - what operator will it be??

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the Sun class SL275 and in that class the instructor introduced us to the mod operator - %. When used (z = x%y) z would be equal to the remainder of the division expression.
int z,x,y;
x = 12;
y = 5;
z = x%y;
z would be 2 (the remainder...)
so now I have been taking all these mock exams and every single one of them (perhaps out of date?!?) refers to this operand (%) as being (z = x%y) z = x - y until z is less than y or something crazy like that... All the helpful tools I have found refer to % as mod... They come up with the same number for z in the above examples... Have I been cramming too much? Is my brain going to EXPLODE? Could someone please clarify if these are the same or not...
Thank you!
AP
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Amy,
The two ARE essentially the same thing .
The description with the subtraction you're probably seeing is something like this:
X%Y:
while (X>Y) X = X-Y
In other words. We keep taking the second operand (Y) away from the first operand (X) until we can't take it away any more. The number we're left with is the result.
e.g. 26%7
26 - 7 = 19
19 - 7 = 12
12 - 7 = 5
Now since we can no longer take away 7 (and have a non-negative answer), the result is 5.
Now think about division with a remainder. X/Y gives us the number of times Y "goes into" X, with a remainder signifying how much is left over. This is exactly what the subtraction example finds. The number of times we subtracted 7 in the above example would be the quotient of 26/7 (3) and the remainder is the same as our result (5).
Matt
 
Amy Petri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you... I think I have gone into PANIC mode and now nothing makes sense to me... You have confirmed what I thought was going on... Now if I can just learn to subtract ..
AP
reply
    Bookmark Topic Watch Topic
  • New Topic