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 Notes

'for' Loop

Purpose

The for statement is similar to the while statement, but it is often easier to use if you are counting or indexing because it combines three elements of many loops: initialization, testing, and incrementing.

General Form

The for and equivalent while statements have these forms.

for (init-stmt; condition; next-stmt) {
    body
}
init-stmt;
while (condition) {
    body
    next-stmt;
}

There are three clauses in the for statement.

  1. The init-stmt statement is done before the loop is started, usually to initialize an iteration variable.
  2. The condition expression is tested before each time the loop is done. The loop isn't executed if the boolean expression is false (the same as the while loop).
  3. The next-stmt statement is done after the body is executed. It typically increments an iteration variable.

Example - Printing a table of squares

Here is a loop written as both a while loop and a for loop. First using while:

int number = 1;
while (number <= 12) {
    System.out.println(number + " squared is " + (number * number));
    number++;
}

And here is the same loop using for.

for (int number = 1; number <= 12; number++) {
    System.out.println(number + " squared is " + (number * number));
}

Example - Counting doubled characters

This code will look at each character in a string, sentence, and count the number of times any character occurs doubled.

String sentence = ...;
int doubleCount = 0;     // Number of doubled characters.

// Start at second char (index 1).
for (int pos = 1; pos < sentence.length(); pos++) (
    // Compare each character to the previous character.
    if (sentence.charAt(pos) == sentence.charAt(pos-1)) {
        doubleCount++;
    }
}

Summary

The for loop is shorter, and combining the intialization, test, and increment in one statement makes it easier to read and verify that it's doing what you expect. The for loop is better when you are counting something. If you are doing something an indefinite number of times, while loop may be the better choice.

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.