Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Summary - Statements

Each control statements is one logical statement, which often encloses a block of statements in curly braces {}. The examples assume the block contains more than one statement.

 

 

 

 

 

 

 

 

 

 

 

 

 

Indenting is essential. Four spaces is most common.

Selection (if, switch)

if Statement

//----- if statement with a true clause
   if (expression) {
       statements // do these if expression is true
   }
   
//----- if statement with true and false clause
   if (expression) {
       statements // do these if expression is true
   } else {
       statements // do these if expression is false
   }
   
//----- if statements with many parallel tests
   if (expression1) {
       statements // do these if expression1 is true
   } else if (expression2) {
       statements // do these if expression2 is true
   } else if (expression3) {
       statements // do these if expression3 is true
   . . .
   } else {
       statements // do these no expression was true
   }
   
   

switch Statement

The effect of the switch statement is to choose some statements to execute depending on the integer value of an expression. The same effect can be achieved with a series of cascading if statements, but in some cases the switch statement is easier to read, and in a some compilers it can produce more efficient code. The break statement exits from the switch statement. If there is no break at the end of a case, execution continues in the next case and this is almost always an error.
   switch (expr) {
      case c1:
            statements // do these if expr == c1
            break;
      case c2: 
            statements // do these if expr == c2
            break;
      case c2:
      case c3:
      case c4:         //  Cases can simply fall thru.
            statements // do these if expr ==  any of c's
            break;
      . . .
      default:
            statements // do these if expr != any above
   }

Loop Statements

while

The while statement tests the expression. If the expression evaluates to true, it executes the body of the while. If it is false, execution continues with the statement after the while body. Each time after the body is executed, execution starts with the test again. This continues until the expression is false or some other statement (break or return) stops the loop.
   while (testExpression) {
       statements
   }

Other loop controls

All loop statements can be labeled, so that break and continue can be used from any nesting depth.
   break;       //exit innermost loop or switch
   break label; //exit from loop label
   continue;    //start next loop iteration
   continue label; //start next loop label
Put label followed by colon at front of loop.
outer: for (. . .) {
         . . .
               continue outer;
   

for

Many loop have an initialization before the loop, and some "increment" before the next loop. The for loop is the standard way of combining these parts.
   for (initialStmt; testExpr; incrementStmt) {
       statements
   }
This is the same as (except continue will increment):
   initialStmt;
   while (testExpr) {
       statements
       incrementStmt
   }

do

This is the least used of the loop statements, but sometimes a loop that executes one time before testing is used.
   do {
      statements
   } while (testExpression);

Other Flow Control Statements

Method Return

   return;      //no value for void method
   return expr; //method value to return

Simple try...catch for exceptions

   try {
       . . . // statements that might cause exceptions
   } catch (exception-type x) {
       . . . // statements to handle exception
   }

throw

   throw exception-object;

Multiple catch clauses and finally clause

Executes first catch clause that specifies the exception class or a super class. The finally clause is always executed (regardless of whethere there was an exception or not) so resources can be cleaned up (eg, closing a file).
   try {
       . . . // statements that might cause exceptions
   } catch (exception-type x) {
       . . . // statements to handle exception
   } catch (exception-type x) {
       . . . // statements to handle exception
   } finally (exception-type x) {
       . . . // statements that will always be exectuted, exception or not.
   }

Copyright 2004 Fred Swartz Last update: 2004-08-18

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.