6 Replies - 430 Views - Last Post: 25 August 2011 - 07:00 AM Rate Topic: -----

#1 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • 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)
            {
                lstDeck.Items.Add(buff);
                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)
            {
                lstDeck.Items.Add(buff);
                buff = " ";
            }
           
        }
        lstDeck.Items.Add("");
    }

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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • 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

Was This Post Helpful? 3
  • +
  • -

#3 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Difference between equality and modulus in an if statement

Posted 24 August 2011 - 08:49 AM

View Postragingben, 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. :)
Was This Post Helpful? 0
  • +
  • -

#4 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • 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))
{

}


Is your code behaving now?
Was This Post Helpful? 0
  • +
  • -

#6 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • 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!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1