Home : Course Map : Chapter 2 : Java : Supplements :
Flow Control
JavaTech
Course Map
Chapter 2

Introduction
Essentials
Structure

Keywords
Primitive Types
Comments
Literals
Expressions
Operators
Statements
Casts & Mixing
Strings
Console Output 
   Demo
Exercises

    Supplements
Conditional: if-else
Repetitions
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The computer steps through the instructions of a program one after another in sequence. However, algorithms require the option of altering the direction of the program flow such as jumping to another section of code in the program.

Java does not offer a "go to" type of statement as in some older languages, because this leads to unreadable spaghetti code. However, Java does contain several ways to jump from one statement to another in certain circumstances. Below we discuss three such statements.

For example, you will occasionally need to jump out of a loop with a break or go back to the top of the loop with a continue statement.

You will also need to use the powerful switch statement that allows for the selection of code segments to jump to according to the value of a variable.

Here we review the these flow control statements.

 

break Statement & Statement Labels

To jump out of a loop when a particular condition occurs, use the break command. As shown here

    while ( i < 5) {
       i=a.doSomething();
       if(i<0) break; // jump out of the loop
    }

the break will result in the program flow moving out of the loop and to the next statement following the loop statement.

If the break occurs within a nested loop, the flow will jump to the next statement in the next outer loop. For example, in the following case

  for( int k=0; k< 10; k++){
    while ( i < 5) {
       i=a.doSomething();
       if(i<0) break; // jump out of the inner while loop
    }
    jj = i;
  }

the break will result in flow jumping to the jj=i; statement.

If, instead, you want the flow to jump outside of both loops, you can put a label on the outer loop and add that name to the break statement.

A label is simply an identifier in front of the loop statement with a colon after it. For example,

  loop1 : for( int k=0; k< 10; k++){
    while ( i < 5) {
       i=a.doSomething();
       if(i<0) break loop1; // jump out of both loops
    }
    jj = i;
  }
  jj = k;

In this case, the program flow will jump out of both loops and go to the statement
jj = k
when the break statement executes.

Note the label does not have to be on the loop statement but does need to be on a statement, such as an if-else statement, that contains the break statement. In this example,

  jmp0: if ( a < b) {
    while ( i < 5) {
       i=a.doSomething();
       if(i<0) break jmp0; // jump out of the loop
    }
    jj = i;
  }
  jj = k;

the break jmp0; will send the flow to the jj = k; statement.

continue Statement

Situations can occur where you don't want to jump out of a loop but simply stop the current iteration and go back to the top and continue with the next iteration.

In the following example

    while ( i < 5 ){
       i=a.doSomething();
       if(i<4) continue;
       b.doSomething();  
    }
the b.doSomething(); statement will not execute until i equals 4 because the continue keeps sending the program flow back up to the next interation of the loop.

In the case of nested loops, you may want to jump out of not only the inner loop but the next one out as well. In that case, you can put a label on the outer loop and jump to it and continue its next iteration, as in

    jmp0: while (b.func()){
      for (int i=0; i<4; i++){
        if( a.func())
continue jmp0;
      }
    }


switch/case Statement

The switch statement provides a powerful tool for selecting code segments according to the value of an integer variable. Multiple if-else statements could do the same work but require extra processing since each if statement will evaluate the variable.

The following example shows the form of the switch statement.

    switch( ii ){
        case 1: statement 1;
        case 2: statement 2;
        default: default statement ;
    }

The ii variable can be a byte, short, int or char type (remember that char data can be treated as unsigned short integers in some situations) but not long.

According to the value of ii, the "case #" will be selected if ii=#. If ii does not equal any of the case values, the default statement will be executed.

So in the above example, if ii equals 1, statement 1 executes. If ii equals 2, statement 2 executes. If ii equals any other value, the default statement executes.

As written above, after statement 1 executes, the program will continue to the next line and execute statement 2 and then the default statement. To prevent this, you can insert a break statement that will send the program flow out of the switch statement and to the statement that follows it:

    switch( ii ){
        case 1: statement 1;
                break;

        case 2: statement 2;
                break;
        default: default statement ;
    }

Note that multiple statements can following the case labels:

  void doSomething( char cc)
  {

    
switch( cc ){
        case 'A':

          doSomething(cc, 2);
          doSomethingElse(cc)
          break;

        case 'B':

          doSomething(cc, 3);

          break;

        default: doSomething(cc, 4) ;
    }
  }

 

References & Web Resources

 

Latest update: Dec.11.2003

            Tech
Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
  
Demo 1
More Mix/Cast
  Demo 2
Exercises

           Physics
Differential Eq.
Euler Method
  
Demo 1
Predictor-Corrector
  
Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.