|
|
|
There is another pretty good way to solve this problem, but it involves a mathematical concept you may not have even heard of - or, at least, not being used in this fashion. And it involves using an if statement anyway - which we didn't see until the next day, hehe.
Most students being taught division for the first time learn the concept of a "remainder".
Then they start doing more precise division allowing for decimals and such and the concept of a remainder is quickly abandoned.
But remainders are useful for computers! We obtain them via modulo operations. Designated in Java as '%'
So, a clever little test if something is odd is to say:
Does 2 evenly divide this number?
If so, it's even. The remainder would be 0.
If not, it's odd. The remained would be 1.
So this:
Is another, completely valid, way to solve the problem.
A good example of if statements from today and a good example that there can be many, many, many ways to approach a problem in programming  _________________
May The Force Be With Us, Always
|
|