Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Iteration

                         

The concept of Iteration has made our life much more easier. Repetition of similar tasks is what Iteration is and that too without making any errors. Until now we have learnt how to use selection statements to perform repetition. Now lets have a quick look at the iteration statements which have the ability to loop through a set of values to solve real-world problems.   

The for Statement

In the world of Java programming, the for loop has made the life much more easier. It is used to execute a block of code continuously to accomplish a particular condition. For statement consists of tree parts i.e. initialization, condition, and iteration.
 initialization : It is an expression that sets the value of the loop control variable. It executes only once.
 condition : 
This must be a boolean expression. It tests the loop control variable against a target value and hence works as a loop terminator. 
 iteration :
It is an expression that increments or decrements the loop control variable.
 Here is the form of the for loop:

for(initialization; condition; iteration){
//body of the loop
}

For example, a sample for loop may appear as follows: 
int i;
for (i=0; i<10; i++)
System.out.println("i = " +i); 

In the above example, we have initialized the for loop by assigning the '0' value to i. The test expression, i < 100, indicates that the loop should continue as long as i is less than 100. Finally, the increment statement increments the value of i by one. The statement following the for loop will be executed as long as the test expression is true as follows:
System.out.println("i = " + i); 

Well, we can add more things inside a loop. To do so we can use curly braces to indicate the scope of the for loop. Like,

 int i;
for (i=0; i<10; i++) {
MyMethod(i);
System.out.println("i = " + i);


There is a simpler way to declare and initialize the variable used in the loop. For example, in the following code, the variable i is declared directly within the for loop: 
for (int i=0; i<100; i++) 
System.out.println("i = " +i); 

Lets see a simple example which will help you to understand for loop very easily. In this example we will print 'Hello World' ten times using for loop.

class printDemo{
  public static void main(String[] args){
    for (int i = 0; i<10; i++){
      System.out.println("Hello World!");
    }
  }
}

Here is the output:

C:\javac>javac printDemo.java

C:\javac>java printDemo
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

After learning how to use a for loop, I would like to introduce another form of for loop to be used for iteration through collection and arrays. This form has enhanced the working of for loop. This is the more compact way to use a for loop. 
Here we will take an array of 10 numbers.

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

The following program, arrayDemo,displays the usage of for loop through arrays. It shows the variable item that holds the the current value from the array.

class arrayDemo{
  public static void main(String[] args){
    int[] numbers = {1,2,3,4,5,6,7,8,9,10};
    for (int item : numbers) {
      System.out.println("Count is: " + item);
    }
  }
}

Here is the output of the program 

C:\javac>javac arrayDemo.java

C:\javac>java arrayDemo
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

We would like to suggest to use this form of for loop if possible. 

                         

Facing Programming Problem?
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:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  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.