Results 1 to 5 of 5

Thread: How to use the break statement in java program?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to use the break statement in java program?

    Hello to all,
    I am new to this forum. I recently started learning java language. In our last lecture we learned break statement. After that our sir asked us to write program using break statement, but none of us able to write that program. That's why I am asking this question on this forum. Can anyone tell me How to use the break statement in java program? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use the break statement in java program?

    There are two types of break statement,one is the unlabeled and other is labeled break. Following program is the example of unlabeled break. Just try to understand it.


    Code:
    package org.kodejava.example.lang;
    
    public class BreakDemo {
        public static void main(String[] args) {
            int[] n1 = {8, 3, 9, 1, 9, 5, 4, 1, 9, 22};
    
            int index1;
            boolean found = false;
    
            int search1 = 7;
            for (index1 = 0; index1 < n1.length; index1++) {
                if (n1[index1] == search) {
                    found = true;
                    break;
                }
            }
    
            if (found) {
                System.out.println(search + " found at index " + index1);
            } else {
                System.out.println(search + " was not found.");
            }
    
        start:
            while (true) {
                for (int x = 0; x < 10; x++) {
                    System.out.print("x = " + x + "; ");
                    if (x == 5) {
                        System.out.println("");
                        break start;
                    }
                }
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to use the break statement in java program?

    Some times what happened that we have to exit from a loop before the completion of the normal loop and in this case we have to use break statement and exit from the loop and loop is terminated. The break statement is used in while loop, do - while loop, for loop and also used in the switch statement.


    Code:
    public class  Break{
      public static void main(String[] args){
        int i,j;
        System.out.println("Prime numbers between 1 to 50 : ");
        for (k = 1;k < 50;k++ ){
          for (l = 2;l < k;l++ ){
            if(k % l == 0)
            {
              break;
            }
          }
          if(k == l)
          {
            System.out.print("  " + k);
          }
        }
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to use the break statement in java program?

    Break statement is used to break flow of normal control and is used to transfer that control to another loop. Just try to understand following example. It is very easy program. Try to understand each step.


    Code:
     
    
          public class JavaBreakExample {
    
     public static void main(String[] args) {
    
     int intArrays[] = new int[]{5,8,3,5,1};
    
    System.out.println("Elements less than 3 are : ");
    
     for(int k=0; k < intArray.length ; k ++)
    
          {
    
          if(intArray[k] == 3)
    
          break;
    
          else
     
          System.out.println(intArray[k]);
    
          }
    
           
    
          }
    
          }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use the break statement in java program?

    Break statement is the powerful feature of java language. It is used to exit from the current running loop. Break statement can be implement with two statements one is for loop and other loop statement. Just try to undersatnd following example.


    Code:
    public class JavaBreakStatementExample {
    
        public static void main(String[] args) {
    
            for(int k=0;k<=20;k++)
            {
                if(k==10)
                {
                    break;
                }
                System.out.println("Break loop :"+k);
            }
        }
    }

Similar Threads

  1. Line break in PHP program
    By Aidan 12 in forum Software Development
    Replies: 4
    Last Post: 04-03-2010, 10:49 PM
  2. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  3. How to use the do-while loop statement in java program?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 07:53 PM
  4. How to use the continue statement in java program?
    By Luz in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 08:40 PM
  5. Program to break out of multiple loops
    By Jaheel in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 12:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,536,794.52698 seconds with 17 queries