# Difference between equality and modulus in an if statement

Page 1 of 1

## 6 Replies - 430 Views - Last Post: 25 August 2011 - 07:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=244647&amp;s=562b0bce75854bc53a9c4ae85b1f1070&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 insanepenguin

Reputation: 7
• Posts: 234
• Joined: 08-July 10

# Difference between equality and modulus in an if statement

Posted 24 August 2011 - 08:26 AM

Hello

Not sure if my title made much sense, here is a better explanation! I have a card shuffler that shuffles and deals out cards. One of the exercises asked me to display cards for a game of euchre so only display 9s 10s jacks queens kings and aces. (displays them in a listbox)

I was wondering how this:

``` if (cardCount % 6 == 0 && cardCount > 0)
{
buff = " ";
}
```

Works fine, I get four lines of six cards but using this: if(cardCount == 6) only displays the first six cards in a line.

cardCount is just a tally of acceptable cards for euchre.

Thanks!

Some more code:

```  deckSize = myDeck.DeckSize;

cardIndex = 1;
cardCount = 0;
buff = "";
while (cardIndex < deckSize + 1)
{
card = myDeck.GetCardIndex();

if(card % 13 < 9 && card % 13 != 0 && card % 13 != 1)
{
myDeck.Incrementer(); // increment card
cardIndex++;
continue;
}

temp = myDeck.getOneCard(cardIndex); // get card to display and increment card
buff += temp + " ";
cardIndex++;
cardCount++;

if (cardCount % 6 == 0 && cardCount > 0)
{
buff = " ";
}

}
}
```

This post has been edited by insanepenguin: 24 August 2011 - 08:27 AM

Is This A Good Question/Topic? 0

## Replies To: Difference between equality and modulus in an if statement

### #2 ragingben

Reputation: 164
• Posts: 612
• Joined: 07-October 08

## Re: Difference between equality and modulus in an if statement

Posted 24 August 2011 - 08:31 AM

Because (cardcount % 6 == 0) is only true when the carcount devided by 6 has no remainder, when (cardcount == 6) will only be true when the card count is 6, no more, no less.

Modulus and equality are very different

This post has been edited by ragingben: 24 August 2011 - 08:32 AM

### #3 Psyguy

• D.I.C Regular

Reputation: 62
• Posts: 273
• Joined: 12-January 11

## Re: Difference between equality and modulus in an if statement

Posted 24 August 2011 - 08:49 AM

ragingben, on 24 August 2011 - 08:31 AM, said:

Because (cardcount % 6 == 0) is only true when the carcount devided by 6 has no remainder, when (cardcount == 6) will only be true when the card count is 6, no more, no less.

Modulus and equality are very different

Couldn't have said it better myself.

### #4 insanepenguin

Reputation: 7
• Posts: 234
• Joined: 08-July 10

## Re: Difference between equality and modulus in an if statement

Posted 24 August 2011 - 08:59 AM

Thanks Ben, when I sat back and looked at it again it makes perfect sense!

cardCount == 6 is only true once as cardCount will keep on counting hence only the first 6 cards are displayed

Whereas (cardCount % 6 == 0 && cardCount > 0) prints a line of cards whenever cardCount / 6 leaves no remainder so 6 % 6, 12 % 6, 18 % 6 and 24 % 6

Have I got that right?

Cheers

### #5 ragingben

Reputation: 164
• Posts: 612
• Joined: 07-October 08

## Re: Difference between equality and modulus in an if statement

Posted 25 August 2011 - 06:16 AM

Hi insanepeguin,

Yep, spot on!

One thing - when I was looking at your code I found it slightly more confusing than it needed to be just because you only use one set of brackets on your if statements. It is pretty good practice to block each condition in brackets, like this:
```if ((card % 13 < 9) && (card % 13 != 0) && (card % 13 != 1))
{

}

```

And sometimes if there are many coditions, or complex conditions I find it useful to split the statement over many lines, again just to create readability:
```if ((card % 13 < 9) &&
(card % 13 != 0) &&
(card % 13 != 1))
{

}

```

### #6 insanepenguin

Reputation: 7
• Posts: 234
• Joined: 08-July 10

## Re: Difference between equality and modulus in an if statement

Posted 25 August 2011 - 06:31 AM

That is good advice, especially as I never remember all the precedence orders!

My code is working now thanks, I see you're in the UK and nearly the same age as me - I like that because I see a lot of teenagers here who could code circles around me lol

### #7 ragingben

Reputation: 164
• Posts: 612
• Joined: 07-October 08

## Re: Difference between equality and modulus in an if statement

Posted 25 August 2011 - 07:00 AM

Haha, and me! Some of the guys on here know so much, I'm constantly finding myself asking "whaaaat?", especially when they are really young. To be honest I'm not exactly a seasoned programmer, upto 3 and a half years a go I was headng for a career as a landscape architect, but somehow found myself working as a programmer!

+1 for the late learners!