---
title: "continue,break"
author: "fantasyday"
date: "2009-07-16T13:25:59Z"
tags: []
slug: "continue-break"
draft: false
lastmod: "2009-07-16T13:26:56Z"
id: 427038
url: "https://fantasyday.iteye.com/blog/427038"
---

The break and continue keywords are used to stop either the entire loop
(break) or just the current iteration (continue).


```java

public class ForLoop {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			System.out.println("Inside loop, i = "+i);
			if (i == 3) {
				System.out.println("if is true: i == 3");
				continue;
				// compile error: says it's unreachable
//				System.out.println("after the continue statement");
			}
			// more loop code, that won't be reached when the above if test is true
			System.out.println("after the if statement, still inside loop");
		}
		System.out.println("out of loop");
	}
}

```


Output:

**Inside loop, i = 0
after the if statement, still inside loop
Inside loop, i = 1
after the if statement, still inside loop
Inside loop, i = 2
after the if statement, still inside loop
Inside loop, i = 3
if is true: i == 3
Inside loop, i = 4
after the if statement, still inside loop
out of loop**





```java

public class ForLoop {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			System.out.println("Inside loop, i = "+i);
			if (i == 3) {
				System.out.println("if is true: i == 3");
				break;
				// compile error: says it's unreachable
//				System.out.println("after the continue statement");
			}
			// more loop code, that won't be reached when the above if test is true
			System.out.println("after the if statement, still inside loop");
		}
		System.out.println("out of loop");
	}
}

```


Output:

**Inside loop, i = 0
after the if statement, still inside loop
Inside loop, i = 1
after the if statement, still inside loop
Inside loop, i = 2
after the if statement, still inside loop
Inside loop, i = 3
if is true: i == 3
out of loop**