Lesson 5:


Arithmetic variables and expressions :

  1. Numeric variables may be declared as int (for integers) or either float, double (for reals)
  2. Addition, subtraction, multiplication, and division of numeric values are performed with the operators +, -, *, /
  3. The value stored in numeric variables may be changed with an assignment statement var = expression;
  4. Values can be added to a variable with var += increment.  Similarly for -= , *=, and /=
     

Boolean expressions and if statement:

  1. 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. 
  2. 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:

  1. Take a look at the file Lesson5.java from the web page.  Try it out.  Try entering both positive and negative values.
  2. 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.)
  3. 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.)