Results 1 to 5 of 5

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

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to use the continue statement in java program?

    Hello to all,
    I am second year B.Sc.I.T. student. I recently started learning java language. In our last lecture we learned concept about continue statement. Our sir told all of us to write java program using continue statement. I tried various method but I unable to write correct program. Can anyone tell me How to use the continue statement in java program?
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

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

    The continue statement has 2 types of forms, one is unlabeled and other is labeled continue statement. The first example of following program you will see how to use the unlabeled continue statement. In the second example you will see how to use the labeled continue statement in java prgram.



    Code:
    package org.kodejava.example.lang;
    
    public class ContinueDemo {
        public static void main(String[] args) {
            int[] numberss = {5, 11, 3, 9, 12, 15, 4, 7, 6, 17};
            int lengths = numberss.length;
            int counters = 0;
    
            for (int k = 0; k < length; k++) {            
                
                if (numbers[k] >= 10) {
                    continue;
                }
    
                counters++;
            }
    
            System.out.println("Found " + countes + " numbers less than 10.");
    
            int[][] datas = {
                    {8, 2, 1}, 
                    {3, 3}, 
                    {3, 4, 5}, 
                    {5, 4}, 
                    {6, 5, 2}};
            int totals = 0;
    
        outer:
            for (int k = 0; l < data.length; k++) {
                for (int l = 0; l < data[k].length; l++) {
                    if (data[k][l] % 2 == 0) {
                        continue outer;
                    }
    
                    totals += data[k][l];
                }
            }
    
            System.out.println("Total = " + totals);
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

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

    Sometimes what happened that we don't need to execute some statements under the loop statement and in this case we use the continue statement. It stops normal flow of the control and helps in transferring control to the loop without executing the statements written after the continue statement. I had written following example which has continue statement. Try to understand it.


    Code:
    public class Continue{
      public static void main(String[] args){
        Thread q = new Thread();
        int aw = 0;
        try{
          for (int k=1;k<10;k++)
          {
            if (k == 5)
            {
              continue;
              
            }
            q.sleep(1120);
            System.out.println("varun");
            System.out.println("Value of w : " + w);
          }
        }
        catch(InterruptedException e){}
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

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

    Using following program you will understand continue statement. First you have to define class "Continue". This program take input fro user and display it on computer screen. Try to understand how continue statement is used in following program. Continue statement is used to transfer control to other code.

    Code:
    import java.io.*;
    
    class Continue{
      public static void main(String[] args) throws Exception{
        int p=2;
        BufferedReader object1= new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter your desired number");
        int s= Integer.parseInt(object.readLine());
        for (int k=0; k<=4; k++){
          if(k==2){
            continue;
          }
          System.out.println("value of p ="+ p);
        }
      }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

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

    The continue statement is used to transfer normal flow control to other condition. It is used to break normal flow control. You can use Continue statement anywhere in the program. Just make sure that it must be at the end of any loop and after that there should have another loop condition. Just try to understand following program.

    Code:
    class ContinueDemo {
        public static void main(String[] args) {
    
            String searchMe = "Welcome o the party";
            int maxx = searchMe.length();
            int n1 = 0;
    
            for (int k = 0; k < maxx; k++) {
                
                if (searchMe.charAt(k) != 'komal')
                    continue;
    
           
                n1++;
            }
            System.out.println("Found " + n1 + " komal's in the string.");
        }
    }

Similar Threads

  1. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  2. 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
  3. How to use the break statement in java program?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 09:27 PM
  4. Java - goto or continue
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 10:47 AM
  5. To use goto statement or not while coding the program?
    By YatinK in forum Software Development
    Replies: 3
    Last Post: 19-02-2009, 07:19 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,248,243.60420 seconds with 17 queries