Java is a popular programming language used for developing various applications. It provides several control statements to control the flow of execution of a program. Two such control statements are break and continue.
The break statement is used to terminate the execution of a loop or switch statement. When a break statement is encountered inside a loop or switch statement, the control is transferred to the statement immediately following the loop or switch statement.
The continue statement is used to skip the current iteration of a loop and move to the next iteration. When a continue statement is encountered inside a loop, the control is transferred to the beginning of the loop for the next iteration.
The syntax of the break statement is as follows:
break;
The break statement can be used inside a loop or switch statement. When a break statement is encountered inside a loop, the loop is terminated and the control is transferred to the statement immediately following the loop. Similarly, when a break statement is encountered inside a switch statement, the switch statement is terminated and the control is transferred to the statement immediately following the switch.
Here is an example of using the break statement inside a loop:
for(int i=1; i<=10; i ){ if(i==5){ break; } System.out.println(i); }
In this example, the loop will terminate when the value of i becomes 5 and the control will be transferred to the statement immediately following the loop.
The syntax of the continue statement is as follows:
continue;
The continue statement can be used inside a loop. When a continue statement is encountered inside a loop, the current iteration of the loop is skipped and the control is transferred to the beginning of the loop for the next iteration.
Here is an example of using the continue statement inside a loop:
for(int i=1; i<=10; i ){ if(i==5){ continue; } System.out.println(i); }
In this example, the value of i will not be printed when it is equal to 5 because the continue statement will skip the current iteration and move to the next iteration.
Java break and continue statements are useful control statements that can be used to control the flow of execution of a program. The break statement is used to terminate the execution of a loop or switch statement, while the continue statement is used to skip the current iteration of a loop and move to the next iteration.