10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14.9 <strong>The</strong> if Statement BLOCKS AND STATEMENTS<br />

372<br />

14.9 <strong>The</strong> if Statement<br />

<strong>The</strong> if statement allows conditional execution of a statement or a conditional<br />

choice of two statements, executing one or the other but not both.<br />

If<strong>The</strong>nStatement:<br />

if ( Expression ) Statement<br />

If<strong>The</strong>nElseStatement:<br />

if ( Expression ) StatementNoShortIf else Statement<br />

If<strong>The</strong>nElseStatementNoShortIf:<br />

if ( Expression ) StatementNoShortIf else StatementNoShortIf<br />

<strong>The</strong> Expression must have type boolean or Boolean, or a compile-time error<br />

occurs.<br />

14.9.1 <strong>The</strong> if–then Statement<br />

I took an early opportunity of testing that statement . . .<br />

An if–then statement is executed by first evaluating the Expression. If the result<br />

is of type Boolean, it is subject to unboxing conversion (§5.1.8). If evaluation of<br />

the Expression or the subsequent unboxing conversion (if any) completes abruptly<br />

for some reason, the if–then statement completes abruptly for the same reason.<br />

Otherwise, execution continues by making a choice based on the resulting value:<br />

• If the value is true, then the contained Statement is executed; the if–then<br />

statement completes normally if and only if execution of the Statement completes<br />

normally.<br />

• If the value is false, no further action is taken and the if–then statement<br />

completes normally.<br />

DRAFT<br />

14.9.2 <strong>The</strong> if–then–else Statement<br />

Did you ever have to finally decide—<br />

To say yes to one, and let the other one ride?<br />

Did You Ever Have to Make Up Your Mind?<br />

An if–then–else statement is executed by first evaluating the Expression. If the<br />

result is of type Boolean, it is subject to unboxing conversion (§5.1.8). If evaluation<br />

of the Expression or the subsequent unboxing conversion (if any) completes

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!