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

前端 未结 13 1691
温柔的废话
温柔的废话 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: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.

提交回复
热议问题