Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Java Program Control

To direct the flow of program execution, Java supports several control-flow statements. These are similar to their C and C++ counterparts and look and act as you would expect. You’ll take a look at each, beginning with the popular if statement.

if Statements

Java supports two types of if statements, both of which require the use of a Boolean expression. This is the syntax for the most basic if statement:

if( boolean expression)
{
    // Do some stuff here
}

Depending on the value of the Boolean expression, the body of the if statement might or might not be executed. If the expression evaluates to true, the body is executed. If the expression evaluates to false, the body of code is skipped.

Take, for example, the following statement:

if( x < 10)
{
    System.out.println("The if block is being executed.\n");
    System.out.println("And the value of x is: " + x);
}

When the preceding statement is encountered, the Boolean expression is evaluated:

x < 10

The body of code following the if statement will execute only in cases in which x is less than 10. If x is equal to (or greater than) 10, the block of code is skipped, and program execution continues immediately after the closing brace (}).

This form of control flow is often called an if-then statement, because it follows the logic of “if the expression is true, then execute this body of code.”

The second form of the if statement uses the keyword else, directing program execution along one of two distinct routes:

if( boolean expression)
{
    // Do stuff here
}
else
{
    // Or else do stuff here
}

If the Boolean value evaluates to true, the block of code immediately following that value is executed. If the expression evaluates to false, the else block is executed.

This form of control flow is often called an if-then-else statement, because it follows the logic of “if the expression is true, then execute this body of code, or else execute this one.”

The preceding example looks like this, using the if-then-else statement:

if( x < 10)
{
    System.out.println( "The if block is being executed.\n");
    System.out.println( "And the value of x is: " + x);
}
else
{
    System.out.println( "The else block is being executed.\n");
    System.out.println( "And the value of x is: " + x);
}

At any given time, only one of the preceding blocks of code will be executed. At no time will both be executed, because the value of the Boolean expression directs flow of execution in only one of two possible directions, not both.

An additional aspect of control can be added to the if statement using the following else-if construct:

if( boolean expression)
{
    // Do stuff here
}
else if( boolean expression)
{
    // Or maybe do stuff here
}
else
{
    // Or else do stuff here
}

Applying this to the earlier example, you can exercise more precise control over the flow of program execution:

if( x < 10)
{
    System.out.println( "The if block is being executed.\n");
    System.out.println( "And the value of x is:" + x);
}
else if( x == 15)
{
    System.out.println( "The else block is being executed.\n");
    System.out.println( "And the value of x MUST be 15");
}
else
{
    System.out.println( "The else block is being executed.\n");
    System.out.println( "And the value of x is: " + x);
}


Caution:  Unlike if statements in C, Java if statement expressions must return a Boolean value. In C, a value of 0 (zero) is treated as false and a value of 1 is treated as true. This is not the case with Java, in which only Boolean expressions can be used in an if statement.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.