Class 11: Conditionals

Back to Binary Representation of Integers. On to Static Methods.

Held: Wednesday, February 8, 2006

Summary: Today we investigate conditional operations in Java.

Overview:

Assignments

Notes:

• Sorry about the lack of reading. We'll see how class goes anyway.
• Some of you seem worried about how you're doing. If you're getting something working for most or all of the lab problems, you're doing fine.

Overview:

• Basic conditionals: the if statement.
• Boolean expressions in Java.
• Lab.

Conditional Control Structures

• The programs we have written to date are fairly simple - They do a sequence of operations and then stop.
• Most programs need additional control structures that affect the flow of control within the program; they select which operations are performed.
• Control structures include conditionals, loops, and method calls.
• The if or conditional statement is one of the simplest control structures.
• Java provides the two basic forms of the if statement.
• The simplest one:
if (test) {
statements;
} // if

• This statement executes the statements only when the test holds.
• The test must return a boolean (truth) value.
• The test must be surrounded by parentheses.
• The braces are optional if you have only a single statement.
• This is a common structure in recipes. For example,
• For fluffier pancakes, decrease the milk by 10%.
• If above 5000 ft, decrease the oven temperature by 25 degrees F.
• It is also possible to suggest an alternate if the test does not hold.
if (test) {
statements;
} // if text
else {
statements;
} // if not test

This command selects between the two sequences of statements. It executes the first sequence of statements if the test evaluates to true and the second sequence if the test evalutes to false.
• Note that Java's if is slightly different from Scheme's.
• Scheme's if selects between expressions and returns a value.
• Java's ifselects between statements and does not return a value.
• Java does not provide a special facility for nesting 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
}

Boolean Values

• So, what kinds of expressions go in the test of a conditional? They must be boolean values.
• boolean is a primitive type, like int or double.
• Booleans are truth values: they are either true or false. They can take on no other values.
• In Scheme, anything that was not false was considered true.
• In Java, you have only those two values.
• In Scheme, these two values were written as #t and #f.
• In Java, these two values are written as true or false. They
• You can compute boolean values with Boolean expressions.
• You can compare two primitive numeric values, x and y, with one of the following.
• 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)
• Note that these differ from Scheme in that they are infix (comes between) rather than prefix (comes before) operations.
• It is possible to combine booleans with Boolean operators,
• b1 && b2 (and)
• b1 || b2 (or)
• !b (not)
• For example, if we wanted to determine whether an integer, i is between 1 and 10, inclusive, we might write
if ((i >= 1) && (i <=10)) {
...
}

• You can typically compare any two objects with the equals method. (It does need to be defined for most classes.)
• For example, to determine if an operator (represented as a string) that you've just read is an asterisk, you might write
if (operator.equals("*")) {
pen.println(x*y);
}

• You can also compare many objects with the compareTo method.
• The form is obj.compareTo(other)
• compareTo returns
• a negative number if obj naturally precedes other
• zero if obj is naturally equal to other
• a negative number if obj naturally follows other
• compareTo is transitive and reflexive.
• For example, to determine whether a BigDecimal, d, is between 0 and 1 (inclusive), we might write
if ((d.compareTo(BigDecimal.ZERO) >= 0) && (d.compareTo(BigDecimal.ONE) <= 0) {
...
}

Lab

• Attempt the lab.
• Finish on your own time.

Back to Binary Representation of Integers. On to Static Methods.

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 9 08:31:30 2006.
The source to the document was last modified on Thu Jan 12 14:58:06 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Outlines/outline.11.html.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu