The break Keyword
"break" is the java keyword used
to terminate the program execution within a block of code that immediately comes
to the currently enclosing block.
In other word we can say that
break keyword is used to prematurely exit a for, while, or do loop or to mark the end of a case block in a
switch statement. Also break keyword is used for terminate a loop. The break always exits
an innermost enclosing while, for, do or switch statement.
The break statement is used to stop a loop for any reason and also the condition.
Examples: following example searches through an array for a particular
value.
switch (n) { case 0: System.out.println("ram"); break; case 3: case 5: case 7: System.out.println(n + " good"); break; case 2: System.out.println(n + " not"); case 4: case 6: case 8: System.out.println(n + " work hard"); break; case 1: case 9: System.out.println(n + "ill"); break; default: System.out.println("ram is work hard."); break; |