Lesson 5:
Arithmetic variables and expressions :
- Numeric variables may be declared as int (for integers) or either float,
double (for reals)
- Addition, subtraction, multiplication, and division of numeric values are
performed with the operators +, -, *, /
- The value stored in numeric variables may be changed with an assignment
statement var = expression;
- Values can be added to a variable with var += increment.
Similarly for -= , *=, and /=
Boolean expressions and if statement:
- Comparisons of numeric values can be made with the operators < ,
> , ==, != , >= , <= . For
example, the operator == returns true
if the values are equal and false
otherwise. The operator != returns true
if the values are not equal and false
otherwise.
- An if statement is of the form
if ( boolean expression) statement
or
if (boolean expression) statement
else statement
The boolean expression evaluates to either true
or false. The following
statement is executed only if the expression is true.
The expression after the else is only executed if the statement is false.
Try this:
- Take a look at the file Lesson5.java from the web page. Try it
out. Try entering both positive and negative values.
- Change the applet so that it will only add values that are both greater
than 0 and less than 100. (You might want to check out the boolean
operators !, && and || on p189 in your text.)
- Add another Label to the applet which prints a message telling whether the
current input value is less, equal or greater than the value which was input
just prior. (You will need to add another variable which stores
the previous input.)