Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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


Up to => Home / Authoring / Scripting / Tutorial




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers