What is the difference between continue and break in java




















As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization. Java uses an expanded form of break which is similar as goto and helps to exit more than one block at a time and resume the control of the program to the end of labeled block, mentioned with the break statement.

The labelled break statement is used to exit from the set of nested loop. As the labelled break statement is encountered, the control exits the labelled block, which is mentioned with the break statement.

In this code, as the control reaches the third block, its print statement gets executed. Label attached with a break is first, so control resume to the statement written after the end of the block first, i. As break terminates the remaining iteration of the loop and lets the control exits the loop. Here, continue works somewhat as a break. The continue statement stops the execution of remaining code in the loop for that current iteration and resumes the control to the next iteration of that loop.

The continue statement skips the code for the current iteration and passes the control to the next iteration of the loop. Whereas, in case of while and do.. The break and continue statement both are the jump statement that transfers the control to another part of the program. Where break statement let the control exit the loop, the continue statement let the control to the next iteration of that loop.

It skips only the current iteration and immediately moves control to next iteration in the loop. Notify me of follow-up comments by email. Notify me of new posts by email. Skip to content. It also terminates the sequence of cases of the switch statement It is used to terminate the execution of the current iteration and jumps to the next iteration of the loop Control It transfers control to the very first statement after the loop It transfers control to the conditional expression of the loop.

Executing 2 times. Executing 3 times. Executing 4 times. Break Encountered! Cat killed! Dog killed! Continue Statement It only terminates the current iteration of the loop and starts next iteration again, without processing statements that come after it within the enclosing loop body. Executing 7 times. Executing 8 times. Is Iterator a Class? What is java classpath? Can a class in java be private? Is null a keyword in java? What is the initial state of a thread when it is started? What is the super class for Exception and Error?

What is Class. Can interface be final? What is the difference between exception and error? What is default value of a local variables? What is local class in java? Can we initialise uninitialized final variable? Can we declare abstract method as final? Can we have finally block without catch block? What is pass by value and pass by reference? Can we declare main method as private? What is the difference between preemptive scheduling and time slicing?

Can non-static member classes Local classes have static members? What are the environment variables do we neet to set to run Java? Can you serialize static fields of a class? What is the difference between declaring a variable and defining a variable? Where can we use serialization? What modifiers are allowed for methods in an Interface? What is the purpose of Runtime and System class?

Which one is faster? ArrayList or Vector? What is the difference between static synchronized and synchronized methods? What is the order of catch blocks when catching more than one exception? What is hashCode? What is the difference between Hashtable and HashMap? What are the restrictions when overriding a method? What is the use of assert keyword? What is adapter class? What is difference between break, continue and return statements?



0コメント

  • 1000 / 1000