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

Loops - Introduction

The purpose of loop statements is to repeat Java statements many times. There are several kinds of loop statements in Java.

while statement - Test at beginning

The while statement is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate.

//... While loop to build table of squares.
String result = "";   // StringBuilder would be more efficient.
int i = 1;
while (i <= 20) {
    result = result + i + " squared is " + (i * i) + "\n";
    i++;
}
JOptionPane.showMessageDialog(null, "Tables of squares\n" + result);

The following example has an assignment inside the condition. Note that "=" is assignment, not comparison ("=="). This is a common coding idiom when reading input.

//... Add a series of numbers.
JOptionPane.showMessageDialog(null, "Enter ints.  Cancel to end");
String valStr;
int sum = 0;
while ((valStr = JOptionPane.showInputDialog(null, "Number?")) != null) {
    sum += Integer.parseInt(valStr.trim());
}
JOptionPane.showMessageDialog(null, "Sum is " + sum);

for statement - Combines three parts

Many loops consist of three operations surrounding the body: (1) initialization of a variable, (2) testing a condition, and (3) updating a value before the next iteration. The for loop groups these three common parts together into one statement, making it more readable and less error-prone than the equivalent while loop. For repeating code a known number of times, the for loop is the right choice.

//... For loop to build table of squares.
String result = "";   // StringBuilder would be more efficient.
for (int i = 1; i <= 20; i++) {
    result += i + " squared is " + (i * i) + "\n";
}
JOptionPane.showMessageDialog(null, "Tables of squares\n" + result);

do..while statement - Test at end

When you want to test at the end to see whether something should be repeated, the do..while statement is the natural choice.

String ans;
do {
    . . .
    ans = JOptionPane.showInputDialog(null, "Do it again (Y/N)?");
} while (ans.equalsIgnoreCase("Y"));

"foreach" statement - Java 5 data structure iterator

Java 5 introduced what is sometimes called a "for each" statement that accesses each successive element of an array, List, or Set without the bookkeeping associated with iterators or indexing.

//... Variable declarations.
JTextArea nameTextArea = new JTextArea(10, 20);
String[] names = {"Michael Maus", "Mini Maus"};

//... Display array of names in a JTextArea.
for (String s : names) {
    nameTextArea.append(s);
    nameTextArea.append("\n");
}

Similar to the 'if' statement

There are three general ideas that you will see in many parts of Java.

  • Braces {} to enclose multiple statements in the body.
  • Indentation to show the extent of the body clearly.
  • Boolean (true/false) conditions to control whether the body is executed.

Scope of loop indicated with braces {}

If the body of a loop has more than one statement, you must put the statements inside braces. If there is only one statement, it is not necessary to use braces {}. However, many programmers think it is a good idea to always use braces to indicate the scope of statements. Always using braces allows the reader to relax and not worry about the special single statement case.

Indentation. All statements inside a loop should be indented one level (eg, 4 spaces), the same as an if statement.

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.