Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

break

Example

Terminate the loop when the value of reaches 4.

for (int i = 0; i < 10; i++) {
 if (i == 4) {
    break;
  }
 System.out.println(i);
}

Definition and Usage

The break keyword is used to break out a for loop, a while loop or a switch block.

More Examples

Example

Exit a while loop prematurely using the “break” keyword.

int i = 0;
while (i < 10) {
  System.out.println(i);
  i++;
  if (i == 4) {
    break;
 }
}