CSCI A201/A597

Lecture Notes 3

Spring 2000


Basic program structure in Java revisited. Constants, identifiers, variables, types, operators, expressions, statements. Assignment statements. Control structures for iteration: the while loop. More control structures: if, for. The element package introduced. Primitive vs. reference types.

Reading assignments (from the text) thus far:

The reading assignments for this set of notes are listed at the end.

Start from the Second program of last lecture, renamed:

public class Example {
  public static void main(String[] args) {
    int i; 
    int sum; 
    i = 0; 
    sum = 0; 
    while (i < 11) {
      sum = sum + i; 
      i = i + 1; 
    }    
  } 
} 
This program computes the sum of the first 10 positive integer numbers. To compute a different sum just change 11 to the number of your choice, and recompile and run the program again. Later we will see how to feed data into the program without the need to recompile.

Can we explain how this program works?

Here's a diagram:

Note that the order of the statements in the body of the loop is important, although in this particular case (we start from 0) does not matter. If there are statements following the loop their execution starts as soon as the loop ends.

There's an alternative description of a program as a flow diagram, that we will exemplify below.

The diagram also exemplifies a short form for the assignment statement that adds some value to a certain variable (or location) that will be detailed below.

Programs are (for now) sequences of statements.

The statements are executed in sequence, one by one.

The kinds of statements we have encountered thus far are:

Assignment statements are distinguished by the assignment operator (=) which does not have its meaning from mathematics. (For that we use the == operator, instead).

On the left of the assignment operator we expect to see an identifier, a name of a storage location, where the result of an expression needs to be stored.

An identifier must begin with a letter, or one of the characters underscore (_) or dollar sign ($). See text page 10. There are a few identifiers that have a predetermined meaning in Java. They are called keywords. Here are some examples: int, boolean, class. You cannot use these keywords in other than precisely the way for which they have been designed.

Both the location where we want to store the result of the expression and the expression itself have a type. For the assignment statement to be valid, the types must be compatible. For now that means they have to be the same.

Our programs so far have manipulated pieces of data. The kinds (or types) of data that we have manipulated thus far were:

For integers we have examined: The expressions used operators.

The integer operators that we have looked at were:

The boolean operators we looked at were: When we compare two integers we in fact write a relational expression whose type (and result) is boolean.

Booleans also can be grouped together using operators such as:

We have also looked at Strings, but mostly as constants.

The named storage locations that we use in assignment statement are called variables, since they can store a value that can change (from one assignment statement to the next).

Constants on the other hand don't change. They're also called literals, since they are exactly what they appear to be.

Java is a strongly typed language (see text page 11-18,) which simply means that every data storage location (variable) that appears in a program must be declared and must have some type.

Java provides two kinds of types:

  1. primitive types
  2. reference types
We have discussed the following primitive types thus far: Strings are not primitive, and for now this should be taken as a matter of fact. We will look at reference types later.

Here are some more operators that could be used to group int (integral) variables or constants into expressions which result in an int value (which is therefore their type):

Of these three the last one can be used to determine if a number is odd or even.

Individual work.

Write a program that sums up the first 10 odd positive integers.
Note: odd positive integers start at 1 and alternate with even positive integers. That's all you need to know about it, and as of right now we won't use %. We'll use it a bit later, below.

Note that in an expression with multiple operators the order of execution is predetermined: for example multiplications are always going to be performed ahead of additions or subtractions, unless the latter are protected by parentheses.

Examples. What's the result of each the following expressions?

6 + 9 / 3

(6 + 9) / 3

6 * 9 / 3

(6 * 9) / 3
Read in the text on page 14 about the order of precedence for the most common operators.

Here now are some more relational operators (that can be used to compare integers to produce a truth value that represents the result of the comparison. This result establishes the relationship between the compared integers):

The logical operators (used to combine boolean constants, or expressions): Here are some examples of boolean expressions:
(3 < 5) && (1 == 1)
This evaluates to true.
(3 < 5) && (1 != 1)
This evaluates to false.
(3 < 5) || (1 != 1)
This evaluates to true.
(3 > 5) || (1 != 1)
This evaluates to false.
!((3 > 5) || (1 != 1))
This evaluates to true.

See pages 24-27 in your text for more details.

The order of evaluation is always left to right for both && and ||, and the evaluation is stopped as soon as the result is known. (This is important when relational expression contains side-effects, although we won't use this approach too often).

The text mentions that the assignment statement also returns a result (or a value) just like any other expression (see page 15).

We also mention here (see page 16) that variables must be declared and initialized before they are used for the first time. Not doing so will generate an error (or more) at compile time).

One could declare more than one variable of the same type in a more compact manner such as this:

int, i, sum;
and one can use the declaration stage as an initialization step as well:
int i = 0, sum = 0;
We now consider additional control structures such as: You can read about the conditional statement in your text on pp. 71-74.

Pages 74-76 review the while loop.

The for loop is covered on pages 80-81 with additional material in the following pages.

These are the only structures that we will look at today. The chapter also contains material about the do-while loop and the switch statement. If you want to read about them on your own you're more than welcome, just make sure you read carefully and ask questions when you think something seems unclear. (Sometimes things can be tricky.)

Please try to read these paragraphs even if some examples make use of the element package. We will provide adequate examples below, and we will cover the element package on Thursday. (We will distribute floppy disks that contain the package in the lab this week and you will have a chance to experiment with it, and write a few basic graphical programs.

The if statement has the following form:

if (<condition>) {
  <statement>;
  <statement>;
  ...
  <statement>;
}
The part in blue is of course, generic.

The sequence of statements will be executed only if the condition evaluates to true.

Can you draw the flow diagram that corresponds to this statement, like we did for the while at the beginning of these lecture notes?

Individual work

Write a program that produces a list of the positive integers less than 30 and marks the ones that are even numbers by writing two asterisks in front of them.
The if-then-else has the following form:
if (<condition>) {
  <statementA1>;
  <statementA2>;
  ...
  <statementAn>;
} else {
  <statementB1>;
  <statementB2>;
  ...
  <statementBm>;
} 
One or the other group of statements (but only one of them) will be executed, based on the value of the tested condition.

Please look at the section on Syracuse numbers in your text (pp. 80-81), since the first homework assignment will be related to them.

The for loop has the following structure:

for (<initialization>; <condition>; <step>) { 
  <statement>;
  <statement>;
  ...
  <statement>;
}
It is equivalent to:
<initialization>; 
while (<condition>;) { 
  <statement>;
  <statement>;
  ...
  <statement>;

  <step>; 
}
We'll give a few examples and explain them.

Essentially a for loop is a more compact while.

Individual work.

Rewrite all the programs that you have written so far with while using for loops instead.
We will also explain expressions (statements) such as: Textbook references: We skip chapter 2 for now, until Thursday.


Last updated: January 18, 2000 by Adrian German