C break keyword

Sometimes there is a situation where we want to jump out of the loop, without checking the loop condition. The break keyword allows us to do this. When the compiler sees break inside any loop, control automatically passes to the first statement after the loop. Thus we can say break takes control outside the immediate block or loop(while, do while, for) or switch case.

You will understand better the use of break keyword from the following example:

Example:

Output:

Here you can see values from 0 to 6 is not printed after i == 3. Because if condition is satisfied. But in if break keyword is used. So the control is passed to an immediate statement.

Progran using break keyword

Output:

Ankit Wasankar

Senior Java Developer
Java 11 | Microservices | Reactjs | SQL

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.