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

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

    Consider an If Else condition. A continue statement executes what is there in a condition and gets out of the condition i.e. jumps to next iteration or condition. But a Break leaves the loop. Consider the following Program. '

    public class ContinueBreak {
        public static void main(String[] args) {
            String[] table={"aa","bb","cc","dd"};
            for(String ss:table){
                if("bb".equals(ss)){
                    continue;
                }
                System.out.println(ss);
                if("cc".equals(ss)){
                    break;
                }
            }
            System.out.println("Out of the loop.");
        }
    
    }
    

    It will print: aa cc Out of the loop.

    If you use break in place of continue(After if.), it will just print aa and out of the loop.

    If the condition "bb" equals ss is satisfied: For Continue: It goes to next iteration i.e. "cc".equals(ss). For Break: It comes out of the loop and prints "Out of the loop. "

提交回复
热议问题