What is the “continue” keyword and how does it work in Java?

前端 未结 13 1567
温柔的废话
温柔的废话 2020-11-22 11:34

I saw this keyword for the first time and I was wondering if someone could explain to me what it does.

  • What is the continue keyword?
  • How
相关标签:
13条回答
  • 2020-11-22 11:53

    A continue statement without a label will re-execute from the condition the innermost while or do loop, and from the update expression of the innermost for loop. It is often used to early-terminate a loop's processing and thereby avoid deeply-nested if statements. In the following example continue will get the next line, without processing the following statement in the loop.

    while (getNext(line)) {
      if (line.isEmpty() || line.isComment())
        continue;
      // More code here
    }
    

    With a label, continue will re-execute from the loop with the corresponding label, rather than the innermost loop. This can be used to escape deeply-nested loops, or simply for clarity.

    Sometimes continue is also used as a placeholder in order to make an empty loop body more clear.

    for (count = 0; foo.moreData(); count++)
      continue;
    

    The same statement without a label also exists in C and C++. The equivalent in Perl is next.

    This type of control flow is not recommended, but if you so choose you can also use continue to simulate a limited form of goto. In the following example the continue will re-execute the empty for (;;) loop.

    aLoopName: for (;;) {
      // ...
      while (someCondition)
      // ...
        if (otherCondition)
          continue aLoopName;
    
    0 讨论(0)
  • 2020-11-22 11:53

    continue is kind of like goto. Are you familiar with break? It's easier to think about them in contrast:

    • break terminates the loop (jumps to the code below it).

    • continue terminates the rest of the processing of the code within the loop for the current iteration, but continues the loop.

    0 讨论(0)
  • 2020-11-22 11:54

    If you think of the body of a loop as a subroutine, continue is sort of like return. The same keyword exists in C, and serves the same purpose. Here's a contrived example:

    for(int i=0; i < 10; ++i) {
      if (i % 2 == 0) {
        continue;
      }
      System.out.println(i);
    }
    

    This will print out only the odd numbers.

    0 讨论(0)
  • 2020-11-22 11:56

    As already mentioned continue will skip processing the code below it and until the end of the loop. Then, you are moved to the loop's condition and run the next iteration if this condition still holds (or if there is a flag, to the denoted loop's condition).

    It must be highlighted that in the case of do - while you are moved to the condition at the bottom after a continue, not at the beginning of the loop.

    This is why a lot of people fail to correctly answer what the following code will generate.

        Random r = new Random();
        Set<Integer> aSet= new HashSet<Integer>();
        int anInt;
        do {
            anInt = r.nextInt(10);
            if (anInt % 2 == 0)
                continue;
            System.out.println(anInt);
        } while (aSet.add(anInt));
        System.out.println(aSet);
    

    *If your answer is that aSet will contain odd numbers only 100%... you are wrong!

    0 讨论(0)
  • 2020-11-22 11:56

    Continue is a keyword in Java & it is used to skip the current iteration.

    Suppose you want to print all odd numbers from 1 to 100

    public class Main {
    
        public static void main(String args[]) {
    
        //Program to print all odd numbers from 1 to 100
    
            for(int i=1 ; i<=100 ; i++) {
                if(i % 2 == 0) {
                    continue;
                }
                System.out.println(i);
            }
    
        }
    }
    

    continue statement in the above program simply skips the iteration when i is even and prints the value of i when it is odd.

    Continue statement simply takes you out of the loop without executing the remaining statements inside the loop and triggers the next iteration.

    0 讨论(0)
  • 2020-11-22 11:58

    The continue statement is used in loop control structure when you need to jump to the next iteration of the loop immediately.

    It can be used with for loop or while loop. The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.

    In case of an inner loop, it continues the inner loop only.

    We can use Java continue statement in all types of loops such as for loop, while loop and do-while loop.

    for example

    class Example{
        public static void main(String args[]){
            System.out.println("Start");
            for(int i=0; i<10; i++){
                if(i==5){continue;}
                System.out.println("i : "+i);   
            }
            System.out.println("End.");
        }
    }
    

    output:

    Start
    i : 0
    i : 1
    i : 2
    i : 3
    i : 4
    i : 6
    i : 7
    i : 8
    i : 9
    End.
    

    [number 5 is skip]

    0 讨论(0)
提交回复
热议问题