[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Back to Exceptions Lab. On to Documentation and JavaDoc.
Held: Wednesday, February 23, 2005
Summary: Today we consider loops (non-recursive control structures for repetition) in Java.
Related Pages:
Notes:
Overview:
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 Exceptions Lab. On to Documentation and JavaDoc.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
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 May 11 10:55:55 2005.
The source to the document was last modified on Mon Jan 24 10:17:07 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005S/Outlines/outline.19.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby