30.06.2013 Views

X - AS Nida

X - AS Nida

X - AS Nida

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Block Statements<br />

Block Statements<br />

The if-else if else Statement<br />

Several statements can be grouped together into a<br />

block statement<br />

A block is delimited by braces : { … }<br />

if (sum < 0)<br />

System.out.print (“less”);<br />

else<br />

System.out.print (“more”);<br />

System.out.println (“ OK”);<br />

When the value of sum is -99 , it prints less OK<br />

When the value of sum is 23 , it prints more OK<br />

10<br />

9<br />

Nested if Statements<br />

Nested if Statements<br />

Block Statements<br />

Block Statements<br />

There could be an if statement within another if or<br />

an else clause<br />

Example<br />

These are called nested if statements<br />

if (sum < 0)<br />

System.out.print (“less”);<br />

System.out.println (“ OK”);<br />

An else clause is matched to the last unmatched if<br />

(no matter what the indentation implies)<br />

Braces can be used to specify the if statement to<br />

which an else clause belongs<br />

if (sum < 0) {<br />

System.out.print (“less”);<br />

System.out.println (“ OK”);<br />

}<br />

When the value of sum is 5,<br />

The 1st prints OK.<br />

The 2nd prints nothing at all.<br />

12<br />

11

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

Saved successfully!

Ooh no, something went wrong!