Title: Use if-then and if-then-else statements.
Course: Java Programming (Oracle) (10055) > Section: Controlling Program Flow

Use of the if-then statement, the most basic form of program flow control, should include

- applying the statement to a program to execute a defined section of code only if a test condition results to true
  - Example: If there is candy in the candy jar, distribute the candy to a student and decrease the candy count.

void distributeCandy() {

// the "if" clause: must have candy
if (isCandy)

{
// the "then" clause: distribute candy to students
numberCandy--;
}

}

If this test evaluates to false (there is no candy), control jumps to the end of the if-then statement. The use of the if-then-else statement includes: The if-then-else statement

- providing a secondary or else path of execution when an if clause evaluates to false.
  - Example: Use an if-then-else statement in the distributeCandy method to take some when there is no candy.

void distributeCandy() {

// the "if" clause: must have candy
if (isCandy)

{
// the "then" clause: distribute candy to students
numberCandy--;
} else

{
System.err.println("The candy jar is empty");
}

}