# Class 06: Conditionals

Back to Even More Classes and Objects. On to Loops.

Held Tuesday, February 2

Summary

Contents

Notes

• Don't forget that lab J3 is due at the start of class tomorrow.
• I've started to separate out the experiment portions of labs. Look at the lab index for more information.
• A few of you have asked how you might access code files if I don't include an explicit link to them. Note that there is a link to `Code` at the top of every online lab.
• Only do J4.2 and J4.4.

## Boolean Values

• In working with Java, we've encountered three kinds of values (more or less)
• Primitive numeric values (`int` and its ilk)
• Built-in object values (`String`)
• User-defined object values (`Point`)
• There are additional kinds of primitive values that we will be concerned with. The simpest of these is the boolean value.
• Booleans are truth values: they are either true or false. They have no other possible values.
• You can compute boolean values with boolean expressions.
• You can compare two primitive numeric values with
• `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)
• You can typically compare any two objects with the `equals` method. (It does need to be defined in most cases.)
• It is also possible to define new 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 nearOrigin() {
return (this.distanceFromOrigin() < 1.0);
} // nearOrigin()
```
• It is also possible to combine booleans with boolean operators,
• `b1 && b2` (and)
• `b1 || b2` (or)
• `!b` (not)
• For example, we might call a point ``bothersome'' if it is within half a unit from either axis. These operations will help 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()
```
• As we'll soon see, there are many uses of boolean values, particularly relative to control structures for programs.
• Control structures influence the flow of control within the program; they select which operations are performed.

## Simple Conditionals

• 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 executes the statement only when the test holds.
• The test must return a boolean (truth) value.
• The test must be surrounded by parentheses.
• This is a common structure in recipes. For example, ``For fluffier pancakes, decrease the milk by 10%.''
• It is also possible to suggest an alternate if the test does not hold.
```if (test) {
statements;
} // if
else {
statements;
} // not test
```
This selects between the two statements. It executes the first if the test evaluates to true and the second if it evalutes to false.
• Java does not provide a special facility for nesting `if` statements. (Some other languages do.)

## More Conditionals

• Often, you need to do more than select between two values, one true and one false.
• While it is possible to do this with a sequence of `if` statements, such a solution is neither readable nor efficient.
• As an alternative, Java provides the `switch` statement (like the `case` statement in Pascal).
• It takes the form
```switch (integer-valued-expression) {
case value1:
stuff-to-do;
break;
case value2:
stuff-to-do;
break;
...
default:
stuff-to-do;
break;
}
```
• If you leave out a `break`, execution will continue into the next `case`.
• You can switch on `byte`, `short`, `int`, `long`, or `char` valued expressions.
• `break` has other uses, which you'll see later.

## Lab

### Reflection

What are some other ways we might use conditionals with the `Point` class?

What are some other ways in which you might expect to use conditionals?

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Filled in the details on Tuesday, February 2, 1999.

Back to Even More Classes and Objects. On to Loops.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.