Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use the continue statement in java program?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-01-2010
Luz Luz is offline
Member
 
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.

Reply With Quote
  #2  
Old 23-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
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);
    }
}
Reply With Quote
  #3  
Old 23-01-2010
Member
 
Join Date: May 2008
Posts: 2,007
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){}
  }
}
Reply With Quote
  #4  
Old 23-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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);
    }
  }
Reply With Quote
  #5  
Old 23-01-2010
Member
 
Join Date: May 2008
Posts: 2,290
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.");
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use the continue statement in java program?"
Thread Thread Starter Forum Replies Last Post
Java program to use enum in switch statement. MADGE25 Software Development 5 01-02-2010 05:12 PM
How to use the do-while loop statement in java program? KALIDA Software Development 5 27-01-2010 06:53 PM
How to use the break statement in java program? Madaleno Software Development 4 23-01-2010 08:27 PM
Java - goto or continue Miles Runner Software Development 5 16-01-2010 09:47 AM
To use goto statement or not while coding the program? YatinK Software Development 3 19-02-2009 06:19 PM


All times are GMT +5.5. The time now is 05:38 AM.