Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The use of % and / Arithmatic Operators

Status
Not open for further replies.
Aug 1, 2003
39
US
Hung up on this... Maybe not seeing it, I don't know.

This Java Loop:

Code:
      for ( int count = 0; count < deck.length; count++ ) 
         deck[ count ] = 
            new Card( faces[ count % 13 ], suits[ count / 13 ] );

In a book that goes with this code above. Pg 310 Dietel, Java How to Program. Seventh Edition. A very good book, by the way

"The calculation (count % 13) always results in a value from 0 to 12(the 13 indices of the faces array in lines 15-1)"
deck.length = 52

Question: How can the count%13 always get a value from 0 to 12? Can someone explain this? Isn't this a remainder operator?

", and the calculation count/13 always results in a value from 0 to 3."
deck.length = 52

Question: How can the result always be from 0 to 3 when I divide any number / 13 it's going to be less than 0.



--------------------------
Thanks
Brian

 
a) You name it - remainder.
b) Integer-arithmetic - the result is truncated to the greatest integer less-or-equals the result:
Code:
for i in {1..52} ; do echo -n $i " "; done 
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  
31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  

for i in {1..52} ; do echo -n $((i % 12)) " "; done 
1  2  3  4  5  6  7  8  9  10  11  0  1  2  3  4  5  6  7  8  9  10  11  0  1  2  3  4  5  6  7  8  9  10  
11  0  1  2  3  4  5  6  7  8  9  10  11  0  1  2  3  4  

for i in {1..52} ; do echo -n $((i / 12)) " "; done 
0  0  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  2  2  2  3  
3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4
when I divide any number / 13 it's going to be less than 0.
Well - can you explain that?


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top