Java Language Basics

Tom Kelliher, CS 116

Sept. 25, 2002

Administrivia

Announcements

Collect lab write-ups.

Assignment

Read over Soda Pop lab.

From Last Time

Finished up Lab 4.

Outline

  1. Arithmetic variables and expressions.

  2. Boolean Expressions and the if statement.

  3. Exercise.

Coming Up

Lab 5.

Arithmetic Variables and Expressions

  1. Numeric variables may be declared as int (for integers) or either float or 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 the if Statement

  1. Comparisons of numeric values can be made with the operators <, >, ==, !=, >=, and <=.

    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 boolean expression is false.

Exercise

Refer to handout.



Thomas P. Kelliher
Tue Sep 24 11:57:14 EDT 2002
Tom Kelliher