Flow Control 

Flow Control
  • Here is a list of flow control structures available in the Java language. 
    • if / else if / else
    • if (age == 28)
      
        {
      
        System.out.println("You are 28!");
      
        }
      
      else if (age == 29)
      
        {
      
        System.out.println("You are 29!");
      
        }
      
      else
      
        {
      
        System.out.println("You're neither!");
      
        }
    • while
    • while (x <=100)
      
        {
      
        X++
      
        System.out.println("x is: " x);
      
        }
      
      // In a while loop, the action is 
      // performed only if the test is true.  
      // An interesting alternative is 
      // the do / while loop that performs 
      // the action "before" testing.
      
      
    • for
    • for (int x = 100;X >=0;x--)
      
        {
      
        System.out.println("x is: " x);
      
        }
Testing a Condition
  • Again, just as in Perl, we can use the ==, !=, >, >=, <, <= operators to test equality. Further, we can use the && and || operators to perform multiple tests. Consider the following: 
  • (8 > 2) && (2 == 3) // Returns false because 
                         // one is false
    
    (8 >= 2) || (2 == 3) // Returns true because 
                         // at least one is true.  
                         // Notice that the second
                         // test is short circuited 
                         // by the true result in 
                         // the first.
               
The Switch Statement
  • However, Java does not stop there. Java introduces another control structure called the switch statement. The switch statement allows you to input a single variable and test for multiple values. Consider the following example 
  • switch (input)
      {
      case 1;
        {
        System.out.println("You entered a 1");
        break;
        }
      case 2;
        {
        System.out.println("You entered a 2");
        break;
        }
      case 3;
        {
        System.out.println("You entered a 3");
        break;
        }
      }
  • You can also use the "default" keyword to specify an action to be performed if none of the other "cases" test true. This is usually used in the case of bad user input. 

  • For example, a program might ask a user to enter a number between 1 and 3. In this situation, the prograsmmer could create a switch structure with cases for user data of 1, 2, or 3. If the user types in 4 however, a "default" clause allows the programmer to handle all other cases. 

    Thus, it would not be strange to see something like the following:

    switch(number)
      {
      case 1:
      do something;
      break;
    
      case 2
      do something;
      break;
    
      case 3
      do something;
      break; 
    
      default
      tell user to type in the correct number
      break
      }
Breaks
  • As you can see, Java also provides a break statement that allows you to break out of a loop. The break statement transfers control out of the enclosing loop (do, for, switch,while) to the statement following the closing bracket. 

Additional Resources:

Operators
Table of Contents
Object Orientation in Java


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory