Introduction
To Java - MFC 158 G
Week
5 Lecture notes - Fall 2000
Chapter 4 / 5 - Control
Structures - MFC 158 (1 of 4)
I.
Algorithm
A.
Procedure to for solving a problem in terms of
1.
Actions to be executed to solve a problem
2.
The order in which they are executed (out of bed, get
dressed, eat breakfast, go to work, etc.)
B.
Pseudocode
1.
An artificial and informal language for developing
algorithms
2.
Allows thinking of the problem, without regard to the
particular language
C.
Flowchart
1.
Graphical representation of an algorithm (or portion of an
algorithm).
2.
Drawn with special-purpose symbols (circles, rectangles,
diamonds, etc.)
D.
Get a thorough understanding of a problem before writing a
program (save time later)
1.
Sketch out the problem
2.
Create pseudocode
3.
Use of flow charts or other graphical representations
(sketches)
II. Control Structures
A.
Sequential - one statement following another statement
B.
Selection - conditionally executing code based upon a
condition (if statements, switch statements)
C.
Repetition - iterate through a section of code until a
particular condition exists (while, do/while and for)
D.
There is no GOTO statement in Java
III.
The if Selection Structure
If
(studentGrade >= 60) System.out.println("passed"); |
If
(studentGrade >= 60) System.out.println("passed"); Else
System.out.println("failed"); |
The
conditional operator (? :) - ternary operator (3 operands)
System.out.println(studentGrade
>= 60 ? "passed" : "failed" ); |
Nested
structures
If
(condition-1) action-1; else if
(condition-2) action-2; else action-default; |
If
(X > 5) // 1. useful where 3 actions are required
based upon conditions // If (Y > 5) // 2. may
want to consider the use of braces after (if x > 5) // System.out.println("x and y are > 5); else System.out.println("x > 5 and y <= 5"); else
System.out.println("x <= 5 and y <= 5"); |
-
To
include several statements in a body of the (if) or (else), use brackets { }
If ( x < y) {action1;
action2; action3;} else {action4;
action5; action6;}
IV. The while repetition
structure - specifies an action to repeat while a condition is true
int
product = 2; while
(product <= 1000) // find the
first power of 2 greater than 1000 { product = 2 * product; // any number of statements between the
braces } |
Sentinel
values - allow a variable number of times to run during runtime (user enter -1)
while
( gradeValue != -1 ) { // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; // prompt for input and read grade
from user input =
JOptionPane.showInputDialog( "Enter Integer
Grade, -1 to Quit:" ); // convert grade from a String to
an integer gradeValue = Integer.parseInt(
input ); } |
V. Assignment operators - abbreviated types
c = c + 3; à c + = 3;
Any
statement of the form:
variable = variable operator expression;
can
be written in the form:
variable operator= expression
(using the binary operators +, -, *, / or
%)
See
table in figure 4.12 on pg 141 for detailed description
VI. Increment and decrement operators (Unary operators)
A.
(c++) à c = c + 1 à c+= 1 (use value,
then increment)
B.
(++c) (increment, then use the value)
C.
(c--) (use value, then decrement)
D.
(--c) (decrement, then use value)
Refer
to page 142 for example usage
VII. Primitive data types - the building blocks for more complex
types
A.
Java is a strongly type language - all variables must have a
type before use
B.
Primitive types are portable in all platforms that support
Java
Refer
to page 144 (figure 4.16) for detailed
table of primitives
VIII.
The for repetition structure - handles details of
counter-controlled repetition
For
(some initial value; loop continuation condition; increment of control
variable)
For
(int counter = 1; counter <= 10; counter++)
System.out.println("The value
is: " + counter);
In
many cases the FOR structure and WHILE structure are interchangable
for (int i = 1; i <= 100;
i++) // count from 1 to 100
for (int i = 100; I >= 1;
I--) // count from 100 backward to 1
Further
examples are on page 165
Cowboy code
comment: "the 3 expressions in the FOR loop are
optional"
- may initialize elsewhere, - may
increment elsewhere
IX. The switch multiple-selection
structure - useful where an algorithm needs to contain a series of decisions in
which a variable is tested separately for each distinct value
switch(
choice ) {
case 1:
System.out.println("you
selected number 1");
break;
case 2:
System.out.println("you selected number 2");
break;
case 3:
System.out.println("you selected number
3");
break;
default:
System.out.println("you
selected an invalid number");
}
// end switch
-
the
break statement causes control to proceed to the first statement after the
switch structure
-
If
no break statement exists, then each time a match is found, then all the other
remaining cases will also be executed
-
Each
case can have multiple actions (no braces required for this structure)
-
If
there is no default case and a case does not exist for a given condition,
nothing will happen
-
Can
only use integer type values in case (no relational, such as >, <, etc)
X. The do/while structure -
similar to while, except always executes at least once
Do {
statement-1;
statement-2;
}
While ( condition );
XI. The break and continue statements
-
they
alter the flow of control
-
if
break is executed in a while, for, do/while or switch structure, causes exit
from the structure
-
for
( count = 1; count <= 10; count++ ) { if ( count == 5 ) break; // break loop only
if count == 5 output += count + " "; } |
-
continue
(used in while) skips the remaining
statements in the body of the structure and continues with the next
iteration of the loop (loop-continuation test evaluated immediately)
-
continue
(used in for) proceeds to next iteration, but increments before loop-continue
test
XII. Logical Operators (AND / OR / NOT)
AND operator
If
(genderCode == 1 && age >= 65)
System.out.println("profile is
senior female"); // where gender
code of 1 = female
OR operator
If
(genderCode == 1 || age >= 65)
System.out.println("profile is at
least - senior or female"); //
gender code of 1 = female
NOT operator
If
(genderCode != 1)
System.out.println("not a
female");
If
( ! (genderCode == 1)
System.out.println("not a
female");
** See truth
tables on page 182 and 184
** Review
Precedence and associatively operators on page 187