Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Back to Conditionals. On to When Things Go Wrong.
Held: Tuesday, 1 April 2003
Summary: Today we consider loops (non-recursive control structures for repetition) in Java.
Related Pages:
Assignments:
Notes:
partition does not always
work correctly (Bad Sam!), so I gave you all full credit for the
problem. Please talk to me if you have other questions.Overview:
if (test) {
stuff-to-do-if-test-holds;
} // if (test)
if (test) {
stuff-to-do-if-test-holds;
} // if (test)
else {
stuff-to-do-if-test-does-not-hold;
} // if (! test)
if (test1) {
stuff-1;
}
else if (test2) {
stuff-2;
}
...
else if (testn) {
stuff-n;
}
else {
default;
} // if none of the tests hold
Rational class that we discussed before break?
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)
while loops.
for loops.
count method look like?
readPass. How did you fix that
problem?
Thursday, 15 January 2003 [Samuel A. Rebelsky]
Tuesday, 1 April 2003 [Samuel A. Rebelsky]
Back to Conditionals. On to When Things Go Wrong.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
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 Tue May 6 09:20:52 2003.
The source to the document was last modified on Tue Apr 1 08:19:59 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.34.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby