COMP 150 - UML Computer Science

UMass Lowell Computer Science 91.460
Java and Distributed Computing
Prof. Karen Daniels
Fall, 2000
Lecture 8
Java Fundamentals
Control Structures
Fri. 9/22/00
Homework #3
HW# Assigned Due
Content
1
Part 1
Part 2
Part 1 & Part 2
Part 1 & Part 2
2
3
Fri, 9/8
Fri, 9/15
Mon, 9/18
Fri, 9/15 Fri, 9/22
Fri, 9/22 Fri, 9/29
Homework is due at the start of lecture on the due date.
Java Fundamentals
Control Structures
Program Control

Sequential execution
 Instructions

executed one after another
Transfer of control
 Something
executed
other than the next instruction is
A Caution

Flow of control has to be carefully thought
out
 “spaghetti”

code is hard to maintain
No “goto” in Java
 goto
is a keyword, but is not used
Single-entry/single-exit
Programming
Every logical “module” has a single defined
entry point and a single defined exit point
 Control passes from the exit point of one
“module” to the entry point of another

 Control
structure stacking
 Control structure nesting
Selection (branching)
if
 if/else
 switch

The “if” Structure

Single selection
 If
condition is true, do some special action
 If condition is false, continue
“if” Syntax
if (boolean expression) statement;

boolean expression
 An

expression that evaluates to true or false
statement
 An

arbitrary Java statement
E.g., could be another if statement
“if” Examples
int j = 2;
int a = 0;
if (j < 3)
{
a = 4;
}
int j = 2;
int a = 0;
if ((j < 3) || (a > 10))
{
a = 4;
j = 55;
if (a+j > 100)
{
// do something
}
}
The “if/else” Structure

Double selection
 If
condition is true, do some special action
 If condition is false, do some other special
action
 Then continue
“if/else” Syntax
if (boolean expression) statement1; else statement2;




statement2 can be its own “if-statement” resulting in “if/else-if/else”
nested logic
An ‘else’ is always paired with a preceding ‘if’
 The most recent previous one unless bracketed otherwise
Similar to conditional operator (?:)
 boolean expression ? if-true-statement : if-false-statement
It is good form to ALWAYS bracket statement1 and statement 2
(even when they are single-line statements)
“if/else” Examples
int j = 2;
int a = 0;
if (j < 3)
{
int j = 2;
int a = 0;
if (j < 3)
{
a = 4;
a = 4;
}
else
{
a = 5;
}
cascading if
}
else if (j < 10) // and >= 3
{
a = 5;
}
else // j >= 10
{
a = 6;
}
The “switch” Structure


The switch structure embodies multiple selection logic
(like a cascading if)
 Do one of an arbitrary number of actions based on the
value of an expression
NOTE: With object-oriented programming, we can use
derived classes and polymorphism to get multiple selection
behavior more elegantly
 Select on object type (class)
 Invoke the same named method of whatever class is
involved at runtime
 More later in the course
“switch” Syntax
switch (integer-expression)
{
case constant-expression :
statement;
…
default :
statement;
}
•Requires condition to be expressible as an int (or convert to an int - e.g.,
byte, char, short)
•Uses labeled cases (also integers)
•Use break to ensure only one case is executed at a time
•Includes support for the C/C++ default label
•Can be nested
“switch” Examples
int n = 10;
final int ONE = 1, THREE = 3, FOUR = 4;
switch (n)
{
case ONE :
Sytem.out.println(“Value is 1”);
break;
case THREE :
case FOUR :
Sytem.out.println(“Value is 3 or 4);
break;
default :
Sytem.out.println(“Value is not 1, 3, or 4”);
break; // not strictly needed, but good form
}
Repetition (iteration, or loops)
for
 while
 do/while

The “for” structure
Basic test-compute-increment control flow
 Includes “built-in” initialization and
increment elements

“for” Syntax
for (initialization; boolean test; increment) action-statement







initialization is executed once upon entry
test is evaluated each time through (including first time)
 If test passes, statement block is executed
 Else, execution continues after for statement
increment is executed after each time through loop, then test is evaluated as
above (“increment” can actually be a “decrement”)
action-statement can be a single statement (or even an empty statement/no-op)
Both initialization and increment can be comma-separated multiple statements
 E.g., for (i = 0, j = 0; i+j < 100; i++, j +=2) { // do something }
loop variables can be declared in the initialization
 Unless you use a comma separated multiple-statement initialization
For good form, ALWAYS bracket the action-statement, even if it is a singleline statement
“for” Examples
for (int i = 1; i < 10; i++)
{
System.out.println(“i = “ + i);
}
for (int i = 10000; i > 0; --i);
for (int i = 10000; i < 0; --i);
for (int i = 1; i < 10; i+=2)
{
System.out.println(“i = “ + i);
}
The “while” Structure



Basic test-compute control flow
Does not have built-in initialization or increment
while loop “compute” statement(s) not guaranteed to
execute
“while” Syntax
while (boolean expression) statement



boolean expression is evaluated
 if true,
 do statement (could be a compound statement)
 control flow then returns to boolean expression (for reevaluation)
 else, control flows to next statement after while statement
No built-in increment!!
For good form, ALWAYS bracket the statement, even if it is a single-line
statement
“while” Examples
int n = 10;
while (n > 0)
{
System.out.println(“n = “ + n);
n--;
}
int n = 10;
while (true)
{
System.out.println(“n = “ + n);
n--;
if (n < 0) { break; }
}
The “do/while” Structure
Basic compute-test control flow
 Loop “compute” statement(s) guaranteed to
execute at least once

“do/while” Syntax
do statement while (boolean expression)




statement is executed
boolean expression is evaluated
 if true, do statement block again
 else, control flows to next statement after do/while statement
No built-in increment!!
For good form, ALWAYS bracket the statement, even if it is a singleline statement

Especially critical here for readability
“do/while” Examples
int n = 10;
do
{
System.out.println(“n = “ + n);
n--;
} while (n > 0);
Related Statement Types
labels
 break
 continue
 return

Labels
Consists of an identifier followed by a colon
 Can be used to label:

a
repetition structure or switch
 a block that is delimited by { }

Can work in conjunction with break and
continue (to modify repetition structure control
logic)
The ‘break’ Statement




Transfer control out of a switch, for, do or while statement
or out of a labeled structure
Only break out of the innermost block (no label specified)
or out of the labeled statement or compound statement
identified by the break
Use basically same as C, C++ except for use with labels
Some programmers avoid the use of break
‘break’ Example
foo:
for (int j = 1; j < 10; j++)
{
for (int k = 1; k < 10; k++)
{
// do something;
if (j+k > 10)
{
break foo;
}
}
}
// do something else
The ‘continue’ Statement





Similar to break (alters control flow in for, do, while loops)
Can be used with a label
With no label - return to the boolean expression and reevaluate
it (the for loop’s iteration statement is executed first)
With label - return to the labeled for, do, or while and
reevaluate as above
Some programmers avoid the use of continue
‘continue’ Example
foo:
for (int j = 1; j < 10; j++)
{
for (int k = 1; k < 10; k++)
{
// do something;
if (j+k > 10)
{
continue foo;
}
}
}
// do something else
The ‘return’ Statement
Exit from a method invocation
 Can return a value (or void)
 Control flows back to the next statement
after the method invocation
