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

break

                         

Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. Using jumping statements like break and continue it is easier to jump out of loops to control other areas of program flow.

We use break statement to terminate the loop once the condition gets satisfied. 
Lets see a simple example using break statement.

class BreakDemo{
  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
      System.out.println(i);
      if (i==3) {
        break ;
      }
    }
  }
}

In the above example, we want to print 5 numbers from 0 to 1 at first using for loop as shown. Then we put a condition that 'if i = = 3', the loop should be terminated. To terminate the loop after satisfying the condition we use break. 
It gives the following output:

C:\javac>javac BreakDemo.java

C:\javac>java BreakDemo
0
1
2
3

The break statement has two forms: labeled and unlabeled. You saw the labeled form in the above example i.e. a labeled break terminates an outer statement. However, an unlabeled break statement terminates the innermost loop like switch, for, while, or do-while statement. 

Now observe the example of unlabeled form below. We have used two loops here two print '*'. In this example, if we haven't use break statement thus the loop will continue and it will give the output as shown below. 

class BreaklabDemo1 
{
public static void main(String[] args) 
{
    for (int i = 0; i < 10; i++) {
    System.out.print("\n");
    for (int j = 0; j<=i; j++)
   {
    System.out.print("*");
    if (j==5)
   {                 // break;
   }
}
}

 

Output:

C:\javac>javac BreaklabDemo1.java

C:\javac>java BreaklabDemo1

*
**
***
****
*****
******
*******
********
*********
**********
C:\javac>

However in the following example we have used break statement. In this the inner for loop i.e. "for (int j=0; j<=i; j++)" will be executed first and gets terminated there n then. Then the outer for loop will be executed i.e. "for (int i=0; i<10; i++)". And it will give the output as shown below.

class BreaklabDemo{
  public static void main(String[] args){
    for (int i = 0; i < 10; i++) {
      System.out.print("\n ");
      for (int j = 0; j<=i; j++){
        System.out.print("*");
        if (j==5)
        break;
      }
    }
  }
}

Output:

C:\javac>javac BreaklabDemo.java

C:\javac>java BreaklabDemo

*
**
***
****
*****
******
******
******
******
******
C:\javac>

continue 

Continue statement is just similar to the break statement in the way that a break statement is used to pass program control immediately after the end of a loop and the continue statement is used to force program control back to the top of a loop. 
Lets see the same example of break statement but here we will use continue instead of break.

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
For Instance,

class continueStatement 
{
public static void main(String[] args) 
{
     for (int i = 0; i < 5; i++) {
     System.out.println(i);
      if (i==3) {
     continue ;
     }
  }

 


C:\javac>javac continueStatement.java

C:\javac>java continueStatement
0
1
2
3
4

C:\javac>

                         

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

Indian Software Development Company

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

Copyright © 2007. All rights reserved.