Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Break Statement

Posted By: Wayne Crawford     Category: Java     Views: 1632

This article explains about Break Statement in java with examples.

Break statement allows you to exit a loop from any point within its body, by passing its normal termination expression. 

When the break statement is encountered inside a loop, the loop is immediately stopped and program control resumes at the next statement following the loop.

Syntax of Break Statement

break;

Example of Break Statement 

Example 1 : Program that displays number from command line and stops when 0 appears

class BreakStatLoop
{
  public static void main(String args[])
  {
int i = 0; = Integer.parseInt(args[0]);
while(i < args.length)
{
if(args[i] == 0)
{
break;
}
System.out.println(Array index " + (i+1) + " has value " + args[i++]);
}
  }
}


// Call from command line

java BreakStatLoop 10 20 30 40 50 0


Output

Array index 1 has value 10
Array index 2 has value 20
Array index 3 has value 30
Array index 4 has value 40
Array index 5 has value 50

  
Share: 


Didn't find what you were looking for? Find more on Break Statement Or get search suggestion and latest updates.

Wayne Crawford
Wayne Crawford author of Break Statement is from Phoenix, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!