next up previous
Next: Nested if's Up: Unit 05 Previous: Scope of variables defined

Use of blocks in an if-else statement

The then-branch or the else-branch of an if-else statement can be any Java statement, and in particular it can be a block.

Example: Given month and year, compute month and year of the next month.

int month, year, nextMonth, nextYear;
...
if (month == 12) {
  nextMonth = 1;
  nextYear = year + 1;
} else {
  nextMonth = month + 1;
  nextYear = year;
}


next up previous
Next: Nested if's Up: Unit 05 Previous: Scope of variables defined