[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 Separating Interface from Implementation. On to Loops.
Held: Wednesday, 15 September 2004
Summary: Today we investigate conditional operations in Java.
Related Pages:
Assignments
Overview:
if
statement.switch
statement.int
, double
, and the ilk)
String
)
Point
,
ThreeWordPhrase
)
boolean
value.
true
or false
. They
can take on no other values.
boolean
values with
Boolean expressions.
x < y
(less than)
x <= y
(less than or equal to)
x == y
(equal to; note there are two
equals signs)
x != y
(not equal to)
x >= y
(greater than or equal to)
x > y
(greater than)
comes between) rather than prefix (
comes before) operations.
equals
method. (It does need to be defined for most classes.)
boolean
methods. For example,
consider Is this point within 1 unit of the origin?
/** * Determine if the current point is within one unit of the origin. */ public boolean isNearOrigin() { return (this.getRadius() < 1.0); } // isNearOrigin()
b1 && b2
(and)
b1 || b2
(or)
!b
(not)
bothersomeif it is within half a unit from either axis. The Boolean operations help us define such a function.
/** * Determine if the current point is bothersome. That is, it is * within half a unit from either axis. */ public boolean bothersome() { return (Math.abs(this.xcoordinate) < 0.5) || (Math.abs(this.ycoordinate) < 0.5); } // bothersome()
if
or conditional statement is one of the
simplest control structures.
if
statement.
if (test) { statements; } // if
boolean
(truth) value.
For fluffier pancakes, decrease the milk by 10%.
If above 5000 ft, decrease the oven temperature by 25 degrees F.
if (test) { statements; } // if text else { statements; } // not test
if
is slightly different from Scheme's.
if
selects between expressions and returns a value.
if
selects between statements and does not return
a value.
if
statements, such as Scheme's cond
.
Instead, use the following:
if (test1) { stuff1 } else if (test2) { stuff2 } else if (test3) { stuff3 } else if (testn) { stuffn } else { default-stuff }
Vec2D
.
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.
Back to Separating Interface from Implementation. On to Loops.
[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:10 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.12.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby