JAVA
control flow statements
Control Flow Statements

The countChars method uses a while statement to loop through all of the characters of the input source and count them: 

      import java.io.*;
      public class Count {
          public static void countChars(Reader in) throws IOException
          {
              int count = 0;

              while (in.read() != -1)
                  count++;
              System.out.println("Counted " + count + " chars.");
          }
          // ... main method omitted ...
      }

   Generally speaking, a while statement performs some action while a
   certain condition remains true. The general syntax of the while
   statement is: 

      while (expression)
          statement

   That is, while expression is true, do statement. In countChars, while
   the read method returns a value that is not -1, the program increments
   count. 
 

   By Convention: The opening curly bracket { is at the end of the same
   line as the while statement, and the closing curly bracket } begins a
   new line aligned with the while, as shown. 
 

   statement can be one statement, as shown in countChars, or it can be
   a statement block. A statement block is a series of legal Java statements
   contained within curly brackets ('{' and '}'). For example, suppose that
   in addition to incrementing count within the while loop you also
   wanted to print the count each time a character was read. You could write
   this instead: 

      . . .
      while (in.read() != -1) {
          count++;
          System.out.println("Read a character. Count = " + count);
      }
      . . .

   A statement such as the while statement is a control flow statement,
   that is, it determines the order in which other statements are executed.
   Besides while, the Java language supports several other control flow
   statements, including: 
 
 

 Statement Keyword
decision making if-else, switch-case
loop for, while, do-while
exception try-catch-finally throw
miscellaneous break, continue, label:, return

Note: Although goto is a reserved word, currently the Java language does not support the goto 
statement. Use Branching Statements instead.

The if-else Statement

      Java's if-else statement provides your programs with the
      ability to selectively execute other statements based on some
      criteria. For example, suppose that your program printed
      debugging information based on the value of some boolean
      variable named DEBUG. If DEBUG were set to true, then your
      program would print debugging information such as the value of
      some variable like x. Otherwise, your program would proceed
      normally. A segment of code to implement this might look like
      this: 

         . . .
         if (DEBUG)
             System.out.println("DEBUG: x = " + x);
         . . .

      This is the simplest version of the if statement: the statement
      governed by the if is executed if some condition is true.
      Generally, the simple form of if can be written like this: 

         if (expression)
             statement

      So, what if you wanted to perform a different set of statements if
      the expression is false? Well, you can use the else statement for
      that. Consider another example. Suppose that your program
      needs to perform different actions depending on whether the user
      clicks on the OK button or the Cancel button in an alert window.
      Your program could do this using an if statement: 

             . . .
             // response is either OK or CANCEL depending
             // on the button that the user pressed
             . . .
         if (response == OK) {
             . . .
             // code to perform OK action
             . . .
         } else {
             . . .
             // code to perform Cancel action
             . . .
         }

      This particular use of the else statement is the catch-all form.
      The else block is executed if the if part is false. There is
      another form of the else statement, else if which executes a
      statement based on another expression. For example, suppose
      that you wrote a program that assigned grades based on the value
      of a test score, an A for a score of 90% or above, a B for a score
      of 80% or above and so on. You could use an if statement with
      a series of companion else if statements, and an else to
      write this code: 

         int testscore;
         char grade;

         if (testscore >= 90) {
             grade = 'A';
         } else if (testscore >= 80) {
             grade = 'B';
         } else if (testscore >= 70) {
             grade = 'C';
         } else if (testscore >= 60) {
             grade = 'D';
         } else {
             grade = 'F';
         }

      An if statement can have any number of companion else if
      statements, but only one else. You may have noticed that some
      values of testscore could satisfy more than one of the
      expressions in the compound if statement. For instance, a score
      of 76 would evaluate to true for two of the expressions in the
      if statement: testscore >= 70 and testscore >= 60.
      However, as the runtime system processes a compound if
      statement such as this one, once a condition is satisfied (76 >=
      70), the appropriate statements are executed (grade = 'C';),
      and control passes out of the if statement without evaluating the
      remaining conditions. 

   The switch Statement

      Use the switch statement to conditionally perform statements
      based on some expression. For example, suppose that your
      program contained an integer named month whose value
      indicated the month in some date. Suppose also that you wanted
      to display the name of the month based on its integer equivalent.
      You could use Java's switch statement to perform this feat: 

         int month;
         . . .
         switch (month) {
         case 1:  System.out.println("January"); break;
         case 2:  System.out.println("February"); break;
         case 3:  System.out.println("March"); break;
         }

     You can also have a default entry at the bottom of the list of the case statements, which is done like this:   default: System.out.println("This isn't a valid month"); break;                  

The break keyword makes the switch statement stop execution, and get out of the switch statement to continue on in the program.    

   Loop Statements

      You were introduced to Java's while statement above. Java has
      two other looping constructs that you can use in your programs:
      the for loop and the do-while loop. 

      Use the for loop when you know the constraints of the loop (its
      initialization instruction, termination criteria, and increment
      instruction). For instance, for loops are often used to iterate
      over the elements in an array, or the characters in a string. 

         // a is an array of some kind
         . . .
         int i;
         int length = a.length;
         for (i = 0; i < length; i++) {
             . . .
             // do something to the i th element of a
             . . .
         }

      You know when writing the program that you want to start at the
      beginning of the array, stop at the end, and hit every element.
      Thus the for statement is a good choice. The general form of
      the for statement can be expressed like this: 

         for (initialization; termination; increment)
             statements

      initialization is a statement that initializes the loop--its
      executed once at the beginning of the loop. termination is an
      expression that determines when to terminate the loop. This
      expression is evaluated at the top of each iteration of the loop.
      When the expression evaluates to false, the for loop
      terminates. Finally, increment is an expression that gets invoked
      for each iteration through the loop. Any (or all) of these
      components can be empty statements (a single semi-colon by
      itself). 

      Java provides another loop, the do-while loop, which is similar
      to the while loop you met earlier except that the expression is
      evaluated at the bottom of the loop: 

         do {
             statements
         } while (booleanExpression);

      The do-while statement is a less commonly used loop
      construct in programming but does have its uses. For example,
      the do-while is convenient to use when the statements within
      the loop must be executed at least once. For example, when
      reading information from a file, you know that you will always
      have to read at least one character: 

         int c;
         Reader in;
         . . .
         do {
             c = in.read();
             . . .
         } while (c != -1);

   Exception Handling Statements

      When an error occurs within a Java method, the method can
      throw an exception to indicate to its caller that an error occurred
      and the type of error that occured. The calling method can use
      the try, catch, and finally statements to catch and handle
      the exception. If you want some more information on this feature 
      that has some more information about throwing and handling exceptions,
      go to Handling Errors Using Exceptions on Sun's java page.  
      
   Branching Statements

      You saw the break statement in action within the switch
      statement earlier. As noted there, break causes the flow of
      control to jump to the statement immediately following the
      current statement. 

      There is another form of break that causes flow of control to
      jump to a labeled statement. You label a statement by placing a
      legal Java identifier (the label) followed by a colon (:) before the
      statement: 

         breakToHere: someJavaStatement

      To jump to the statement labeled breakToHere use this form
      of the break statement. 

         break breakToHere;

      Labeled breaks are an alternative to the goto statement, which is
      not supported by the Java language. 

      Use the continue statement within loops to jump to another
      statement. 

      Note: The continue statement can only be called from within
      a loop. 

      The continue statement has two forms: unlabeled and labeled.
      If you use the unlabelled form, control transfers to the
      termination condition of the loop. The termination condition gets
      re-evaluated at this point and the loop either continues or not
      depending on the results of the test. In for and while loops,
      this means that control returns to the top of the loop. In
      do-while loops, this means that controls returns to the bottom
      of the loop. 

      If using the labeled form of continue, the loop continues at the
      labelled statement. Consider this implementation of the String
      class's indexOf method which uses the labeled form of
      continue: 

         public int indexOf(String str, int fromIndex) {
             char[] v1 = value;
             char[] v2 = str.value;
             int max = offset + (count - str.count);
           test:
             for (int i = offset + ((fromIndex < 0) ? 0 : fromIndex); i <= max ; i++) {
                 int n = str.count;
                 int j = i;
                 int k = str.offset;
                 while (n-- != 0) {
                     if (v1[j++] != v2[k++]) {
                         continue test;
                     }
                 }    
                 return i - offset;
             }
             return -1;
         }

      The last of Java's branching statements the return statement.
      You use return to exit from the current method and jump back
      to the statement within the calling method that follows the
      original method call. There are two forms of return: one that
      returns a value and one that doesn't. To return a value, simply
      put the value (or an expression that calculates the value after the
      return keyword): 

         return ++count;

      The value returned by return must match the type of method's
      declared return value. 

      When a method is declared void use the form of return that
      doesn't return a value: 

         return;
 

Onto Page 5

This page was created by Bob Perini 
For CS 763(Web Seminar) at SUNY Buffalo