[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Links]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.4.2 API]
[EIJ]
[CS152 2000F]
[CS153 2004S]
Back to Conditionals. On to Laboratory: Java Basics, Revisited.
Held: Friday, 17 September 2004
Summary: Today we consider loops (non-recursive control structures for repetition) in Java.
Related Pages:
Due
Assignments
Notes:
Overview:
if statements, such a solution is neither readable nor
efficient.
switch statement
(like the case statement in Pascal).
switch (integer-valued-expression) {
case value1:
stuff-to-do;
break;
case value2:
stuff-to-do;
break;
...
default:
stuff-to-do;
break;
}
break, execution will continue into
the next case.
byte, short, int,
long, or char.
break statement has other uses, which you'll see later.
while loops, which have the form
while (test) {
statements;
} // while
next-statement;
boolean done = false;
SimpleOutput pen = new SimpleOutput();
SimpleInput eyes = new SimpleInput();
int nextGrade = 0;
int numGrades = 0;
int sumGrades = 0;
while (!done) {
pen.print("Enter the next grade: ");
nextGrade = eyes.readInt();
if (nextGrade != 0) {
sumGrades = sumGrades + nextGrade;
numGrades = numGrades + 1;
}
else {
done = true;
}
} // while (!done)
done is often called the loop sentinel,
a variable upon which repetition of the loop continues.
continue sentinel.
int continue = 1;
while (continue) {
pen.print("Enter the next grade: ");
nextGrade = eyes.readInt();
if (continue = (nextGrade != 0)) {
sumGrades = sumGrades + nextGrade;
numGrades = numGrades + 1;
} // if (continue = (nextGrade != 0)
} // while (continue)
do loop. This variant has the form
do {
statements
} while (test);
next-statement;
do {
pen.print("Enter the next grade: ");
nextGrade = eyes.readInt();
if (nextGrade != 0) {
sumGrades = sumGrades + nextGrade;
numGrades = numGrades + 1;
}
} while (nextGrade != 0);
result = 1
for each value, v, between 1 and N
multiply result by v, updating result
return the final result
for (initialization; test; increment) {
statements;
} // for
initialization;
while (test) {
statements;
increment;
} // while (test)
/**
* Compute n!.
*/
public int factorial(int n) {
int i; // A counter variable
int factorial; // The factorial of n
factorial = 1;
for (i = 1; i <= n; i = i+1) {
factorial = factorial * i;
} // for
return factorial;
} // factorial(int)
var, or the
postincrement operator,
var++.
for (i = 1; i <= n; ++i)
for (int i = 1; i <= n; ++i)
Back to Conditionals. On to Laboratory: Java Basics, Revisited.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Links]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.4.2 API]
[EIJ]
[CS152 2000F]
[CS153 2004S]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Dec 8 10:37:11 2004.
The source to the document was last modified on Thu Aug 26 20:22:22 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/Outlines/outline.13.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby