Computer Applications
How are these statements different from each other?
i. break
ii. continue
iii. System.exit(0)
iv. return
Answer
(i) The break statement terminates the current loop or switch statement. The execution then continues from the statement immediately following the current loop or switch statement.
(ii) The continue statement tells the computer to skip the rest of the current iteration of the loop. However, instead of jumping out of the loop completely like break statement, it jumps back to the beginning of the loop and continues with the next iteration. This includes the evaluation of the loop controlling condition to check whether any further iterations are required.
(iii) Unlike break and continue statements which are used to control the flow of execution, System.exit(0) command terminates the execution of the program by stopping the Java Virtual Machine which is executing the program. It is generally used when due to some reason it is not possible to continue with the execution of the program.
(iv) A return statement is used to end the execution of a method and return a value to the calling method, optionally.
Related Questions
State whether the following statement is True or False :
The statements that facilitate unconditional transfer of control are called jump statements.
What are loop control structures? What are the essential segments of a loop control structure?
Identify all the errors in the following statements.
for (int i = 5; i > 0; i++) { System.out.println("Java is fun!"); }Identify all the errors in the following statements.
while (x < 1 && x > 50) { a = b; }