Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Conditional execution page 14 of 37


So far, within each method we've used sequential execution only, executing one statement after another. Like other languages, Java(TM) provides language constructs for conditional execution, specifically, if, switch, and the conditional operator ?.

Conditional Constructs
if (<boolean-expression>)
<statement>...
else
<statement>...
switch (<expression>) {
case <const-expression>:
<statements>...
break;

more-case-statement-break-groups...

default:
<statements>...
}
(<boolean-expression>) ? <if-true-expression>
: <if-false-expression>

The more general construct, if has the syntax:


if (<boolean-expression>)
  <statement>...

where <statement>... can be one statement, for example,


x = 4;

or multiple statements grouped within curly brackets (a statement group), for example,


{
  x = 4;
  y = 6;
}

and <boolean-expression> is any expression that evaluates to a boolean value, for example,

Boolean ExpressionInterpretation
x < 3x is less than 3
x == yx is equal to y
x >= yx is greater than or equal to y
x != 10x is not equal to 10
<variable>variable is true

If the Boolean expression evaluates to true, the statement (or statement group) following the if clause is executed.

Java also supports an optional else clause; the syntax is:


if (<boolean-expression>)
  <statements>...
else
  <statements>...

If the Boolean expression evaluates to true, the statement (or statement group) following the if clause is executed; otherwise, the statement (or statement group) following the else clause is executed.

Boolean expressions often include one or more Java comparison operators, which are listed in the following table:

Comparison OperatorInterpretation
<less than
<=less than or equal to
>greater than
>=greater than or equal to
==equal to
!=not equal to

Returning to our user-defined type Dog, we can add additional state variables for more flexible representation of real-world objects. Suppose we add instance variables gentle and obedienceTrained, which can be true or false:


class Dog {
  String barkSound = new String("Woof.");
  boolean gentle = true;
  boolean obedienceTrained = false;
  ...

In Java, Boolean values are literals (case is significant), and boolean variables accept either value. For gentle and obedienceTrained there are no new operators because we're not creating objects--we're creating primitive variables and assigning them the default values true and false.

Access methods provide the flexibility for modifying these instance variables on a dog-by-dog basis:


  void setGentle(boolean gentle) {
    this.gentle = gentle;
  }

  void setObedienceTrained(boolean trained) {
    obedienceTrained = trained;
  }

Note that the reference to obedienceTrained in setObedienceTrained() does not require qualification with this because there is no local variable by the same name.


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact