|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
switch StatementsThe switch statement is similar in nature to the if-then-else statement, although it makes the programmers job a lot easier when multiple else clauses are needed: switch( expression) { case constant1: // Do stuff if the expression evaluates to constant1 break; case constant2: // Do stuff if the expression evaluates to constant2 break; case constant3: // Do stuff if the expression evaluates to constant3 break; default: // Do stuff here if the expression doesn't evaluate // to any of the explicit cases break; } Upon entering a switch statement, the expression is evaluated. The data type of the expression must be char byte, short, or int. Boolean expressions arent allowed, although they are used in all other control-flow mechanisms. The value of the expression is then converted to the int type, as are all the case constants. Beginning with the first case statement, the value of the expression is compared to the value of the case constant. If the two values are equal, any code following the colon is executed, until the break statement is reached. If the expression doesnt match the case constant, it is compared to the next one. This process continues until the default case is reached, at which point the code for this case is executed. When a case is executed, the break statement is used to stop the flow of execution. When a break statement is reached, execution stops immediately and resumes after the closing brace (}) of the switch body. Because execution terminates when the first break is encountered, the default case will be executed only if no match is found between the value of the switch expression and all other cases. The switch statement is particularly useful when multiple cases exist. If you were to try to write more than a half-dozen else clauses in an if-else statement, youd find the process a bit tedious. It would be even more difficult to read the code. With a switch statement, however, the code is clean and easy to read. You can use as many cases as you need, without making a mess of the code. And if none of the cases matches the value of your expression, you can rely on the default case being executed.
Programming with LoopsControl-flow statements include a number of loops:
Java while and do-while loops are identical to those in C: while( boolean expression) { // Do something } do { // Do something } while( boolean expression) In the while loop, the Boolean expression is evaluated. The value of this expression determines whether the body of the loop is executed. If the expression evaluates to true, the loop is executed. If its false, it does not. Each time through the body of the while loop, the expression is reevaluated. The loop continues until the expression evaluates to false: int x = 0; while( x++ < 10) { System.out.println( "The while loop is being executed.\n"); System.out.println( "And the value of x is: " + x); } In the preceding example, the body of the loop continues executing as long as x is less than 10. Because you increment the value of x by one (x++) in the expression itself, the loop is executed 10 times. Note that the increment could have also taken place in the body of the loop itself, as shown here: while( x < 10) { x++; System.out.println( "The while loop is being executed.\n"); System.out.println( "And the value of x is: " + x); } With the do-while loop, the body of the loop executes once before the expression is ever evaluated. This ensures that your loop code is executed at least once, regardless of how the expression evaluates. Heres an example: do { System.out.println( "The while loop is being executed.\n"); System.out.println( "And the value of x is: " + x); } while( x++ < 10); As with the while loop, you could have incremented the expression inside the loop body rather than inside the expression: do { System.out.println("The while loop is being executed."); System.out.println( "And the value of x is: " + x); x++; } while( x < 10); The while loop is by far the most popular of the two, although the do-while loop has the advantage of executing your code at least once. Be sure to change your expression value either inside of the body of the while or do-while loop, or in the expression itself. If the value never changes, the expression always remains true and the loop executes indefinitely. The for loop repeats program execution as controlled through a series, which terminates when a certain limit is reached. It continues looping until the specified limit is reached, at which point the loop is broken and execution resumes after the loop body: for( expression; booleanExpression; expression) { // Do something } The first expression initializes the loop variable. The second is a Boolean expression that specifies the limit. The third and final expression specifies how the loop variable is to change each time through the loop. Consider the following example: int x; for( x=0; x<10; x++) { System.out.println( "The for loop is being executed.\n" ); System.out.println( "And the value of x is: " + x); } The first expression, x=0, sets the loop variable to zero. The loop executes until the second expression, x<10, evaluates to true. And the final expression, x++, increments the loop variable by one every time through the loop. Unlike C, Java supports the declaration of loop variables inside the initialization portion of the loop statement: for( int x=0; x<10; x++) { System.out.println( "The for loop is being executed.\n"); System.out.println( "And the value of x is: " + x); } In this case, the scope of the loop variable is the loop itself. Youre free to access x, as long as you do so in the body of the for loop. However, you cant use x outside of the closing loop brace (}), because anything outside the loop is beyond the scope of this variable.
|
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. |