Computer Science Canada Even/Odd If Statements |
| Author: | Zampano [ Wed Sep 19, 2007 7:46 am ] |
| Post subject: | Even/Odd If Statements |
Hey guys, I just started in Turing, and because I finished the class's assigned programming my teacher gave me a bonus question to solve which involves code I don't know about. The assignment is to create a two-player game with alternating turns (the rest of the programming assignment is irrelevant) and I am not sure how I could reuse the game's code and still have alternating turns on that game. My question eventually boils down to this: How can I ask if a number is even in an if statement? What I tried was: tries:=tries+1 and later: if tries=even then put "Player two, go." However, nothing I tried worked. Is there a simpler way? Thanks guys. |
|
| Author: | neufelni [ Wed Sep 19, 2007 7:59 am ] | ||
| Post subject: | RE:Even/Odd If Statements | ||
You will need to use the mod command. It determines the remainder of a division. And since all even numbers have a remainder of 0 when divided by 2, it's as simple as this:
|
|||
| Author: | Zampano [ Wed Sep 19, 2007 8:31 am ] |
| Post subject: | Re: Even/Odd If Statements |
I cannot thank you enough. Lol, the class I had to do it in hadn't even finished and you responded so quickly. Thanks. |
|
| Author: | Tony [ Wed Sep 19, 2007 12:23 pm ] | ||
| Post subject: | RE:Even/Odd If Statements | ||
Just no cheating, ok? Another really cool way of alternating turns has to do with the property that -1 * -1 = 1. So instead of incrementing the step and figuring out the remainder each time, you just flip the sign.
|
|||
| Author: | Bored [ Thu Sep 27, 2007 9:59 am ] | ||
| Post subject: | Re: Even/Odd If Statements | ||
Or along the same idea, you could use a boolean value and change it each time.
|
|||
| Author: | Zampano [ Thu Sep 27, 2007 10:09 am ] |
| Post subject: | Re: Even/Odd If Statements |
No cheating: goes without saying. The assignment is not an inclass test, but rather the question was assigned in class, and I got a response before that same class ended, which meant I was able to make the whole program in one class. Thanks again guys! |
|