Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
  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 a
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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>

                         

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 
Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

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 | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.