The break keyword is a very interesting concept and a very useful keyword. As the name says, it is used to break, out of the loop.
Now, let’s have a look at the below example, which is used to demonstrate the use of the break keyword. –
In the above example, for some reason, we are breaking out of the loop, when the value of i is 5(i do not know why we are even doing this, but yeah, we understand the break keyword this way). So, in the above program, the loop runs with peace of mind, and then, when the value of i becomes 5, the condition becomes true, the break keyword executes, and we are out of the loop. Have a look at the output –
As you can see, the break statement was executed, and as soon as this happened, we are out of the loop. This break keyword is pretty interesting and useful, and you can use the break keyword as and when required. You can also practice the programs, which involve the break keyword somehow, to get familiar with the break keyword.