Saturday, November 16, 2019

Java_Lesson(5) Math Objects

Of course, this was coming. A simple switch to 'input'
as an int will allow one to work with mathematical expressions
such as <= for equal or lesser than.


Predictably, user entering decimals will earn us an exception. We could
just work with input as a double, but doing this as a policy amounts to
using too much memory allocation.


The answer is using a try/catch construct. The first condition
will play out if the user entered an int. Alternatively, instead of the
program crashing out in the decimals case, we show an error message
to the user. Storing the value from the user must happen within the
try curly braces!


The program as a whole can proceed once this hurdle passed.


The much maligned do... while loop:

                 
                                                   *     *     *

Why is do...while controversial!? Many student programmers 
have stumbled on it to create an infinite loop (one has to shut
the program down to stop it). The source of error is that a boolean
variable only takes the values true or false. But myBoolean false
(!myBoolean) says that the variable has yet to be initiated ie given a value
to store in memory.

One could actually run the above with correct = false; and it would work the
same.

And for anyone tempted to tamper with (!correct), that's the route to perdition 
with the infinite loop.

It's an oooold joke.



No comments: