30.06.2013 Views

X - AS Nida

X - AS Nida

X - AS Nida

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

The if Statement<br />

The if Statement<br />

The if Statement<br />

The if Statement<br />

An example of an if statement:<br />

The if statement has the following syntax:<br />

if (sum < 0)<br />

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

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

The condition<br />

must be a boolean<br />

expression. It<br />

must evaluate to<br />

either true or false.<br />

if is a Java<br />

reserved word<br />

if ( condition )<br />

statement;<br />

The condition sum < 0 is evaluated.<br />

If the condition is true, less OK is printed.<br />

If it is not, OK is printed.<br />

If the condition is true, the statement is executed.<br />

If it is false, the statement is skipped.<br />

6<br />

5<br />

The if-else if else Statement<br />

Boolean Expressions<br />

Boolean Expressions<br />

An else clause can be added to an if statement to<br />

make an if-else statement<br />

A condition often uses one of Java's equality<br />

operators or relational operators, which all return<br />

boolean results:<br />

if ( condition )<br />

statement1;<br />

else<br />

statement2;<br />

== equal to<br />

!= not equal to<br />

< less than<br />

> greater than<br />

= greater than or equal to<br />

If the condition is true, statement1 is executed; if<br />

the condition is false, statement2 is executed<br />

One or the other will be executed, but not both<br />

Note the difference between the equality operator<br />

(==) and the assignment operator (=)<br />

8<br />

7

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

Saved successfully!

Ooh no, something went wrong!