Home  >  Article  >  类库下载  >  Break and continue in java jump out of the specified loop

Break and continue in java jump out of the specified loop

高洛峰
高洛峰Original
2016-10-20 11:36:222410browse

 break and continue in java can jump out of the specified loop

 break and continue in java can jump out of the specified loop. If no loop name is added after break and continue, it will jump out of the loop by default. If you add the specified loop name after it, you can jump out of the loop. Specify a loop (the specified loop is usually an outer loop nested within a loop).

  Break jumps out of the specified loop sample code:

loop1:for(int x = 0; x < 4; x++)
        {
            loop2:for (int y = 0; y < 5 ; y++ )
            {
                System.out.println("x="+x+",y="+y);
                if (y==2)
                {
                    break loop1;
                }
            }
        }

Code running results:

Break and continue in java jump out of the specified loop

Continue Jumps out of the specified loop sample code:

loop1:for(int x = 0; x < 4; x++)
        {
            loop2:for (int y = 0; y < 5 ; y++ )
            {    if (x==2)
                {
                    continue loop1;
                }
                System.out.println("x="+x+",y="+y);
            }
        }

Code running results:

Break and continue in java jump out of the specified loop

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:socket programmingNext article:socket programming