eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Flow Control  
  • Just as was the case with Perl yesterday, Java provides several methods to control flow. Fortunately, control flow works exactly the same way in Java as it works in Perl, so we don't have to learn too many new things in this section.

  • The following list should remind you of the flow control structures available in Perl which are also available in Java

    • 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;
        }
      }
    

Breaks

  • As you can see, Java also provides a break statement that allows you to break out of a loop.
Previous Page | Next Page