Tutorialslot.com

youtube facebook twitter gmail pinterest

Break Statement in Java

Break:
It has two forms;labelled and unlabelled.The break statement can be used in three ways.
1)Used for terminating statement sequence(eg.switch statement)
2)Used to exit a loop
3)Used as a another form of "goto" statement.

Example:
public class Break
{
public static void main(String[] args)
{

   int[] arrayOfInts = { 32, 87, 33, 589, 12, 10,2000, 78, 629, 125 };
    int searchfor = 10;

   int i = 0;
   boolean foundIt = false;

   for ( ; i < arrayOfInts.length; i++)
  {
    if (arrayOfInts[i] == searchfor)
    {
     foundIt = true;
     break;
   }
   }

   if (foundIt)
  {
     System.out.println("Found " + searchfor + " at index " + i);
   }
  else
  {
     System.out.println(searchfor + "not in the array");
  }
}
}

Output:
Found 10 at index 5



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

Java java tutorials learn java java programming tutorial learn java online learning java professionals tutorials Multithreading Generics Object Oriented Language Methods Polymorphism Inheritance