if statement

Purpose:

To choose among alternate courses of action in a program.

Mechanics:

if (<condition>) {
/* Java statements */
}

Example:

if (numA == numB) {
/* Java statements */
}

Usage:

  • <condition> is any expression that evaluates to true or false.

Restrictions:

  • First <condition> is evaluated. When <condition> evaluates to true, the /* Java statements */ are executed. When <condition> evaluates to false, execution continues with the statement after the conditional code.
  • If there is more than one statement in the if clause, then curly brackets must be used. If there is only one statement, the curly brackets are optional.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu