Programming References and tutorials for Developers.

Java Two Control Statements

By The Saint on Thursday, April 02, 2009

Filed Under:

Java Two Control Statements
Inside a method, execution proceeds from one statement to the next, top to bottom. However, it is possible to alter this flow through the use of the various program control statements supported by Java. Although we will look closely at control statements later, two are briefly introduced here because we will be using them to write sample programs.
The if Statement
You can selectively execute part of a program through the use of Java’s conditional statement: the if. The Java if statement works much like the IF statement in any other language. Its simplest form is shown here:

if(condition) statement;

Here, condition is a Boolean expression. If condition is true, then the statement is executed. If condition is false, then the statement is bypassed. Here is an example:

if(10 < 11) System.out.println("10 is less than 11");

In this case, since 10 is less than 11, the conditional expression is true, and println( ) will execute. However, consider the following:

if(10 < 9) System.out.println("this won't be displayed");

In this case, 10 is not less than 9. Thus, the call to println( ) will not take place.

Java defines a full complement of relational operators that may be used in a conditional expression. They are shown here:

Operator Meaning
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
= = Equal to
!= Not equal

0 comments for this post