Next: None. Up: Booleans and breaks. Previous: Using the boolean type.


break statement

Textbook: Section 4.8

The break statement is useful when you want to immediately terminate from a loop. In fact, in our has_guessed example, we could have used a break statement instead.

while(true) {
    IO.println("Guess a number. ");
    if(IO.readInt() == 32) break;
}
IO.println("You got it!");
This works the same way: It continues asking the user to guess a number, until finally the user types 32. When the user types 32, the computer executes the break statement, which breaks out of the while loop and continues with the following statements.

Exercise


Next: None. Up: Booleans and breaks. Previous: break statement.