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

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

    I'm a bit late to the party, but...

    It's worth mentioning that continue is useful for empty loops where all of the work is done in the conditional expression controlling the loop. For example:

    while ((buffer[i++] = readChar()) >= 0)
        continue;
    

    In this case, all of the work of reading a character and appending it to buffer is done in the expression controlling the while loop. The continue statement serves as a visual indicator that the loop does not need a body.

    It's a little more obvious than the equivalent:

    while (...)
    { }
    

    and definitely better (and safer) coding style than using an empty statement like:

    while (...)
        ;
    

提交回复
热议问题