Difference between break and continue

Very simple but yet confusing to beginners.
"Break quits the loop without executing the rest of the statements in the loop. Continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration"



This example may provide a better insight

public class BreakAndContinue {
    public static void main(String[] args)
    {
        for(int i=0; i<100; i++){
            if ( i == 80)break;
            if (i%10!=0)continue;
            System.out.println(i);
        }
    }

}


also there is no notorious "goto" in java, after all who needs a feature in programming language that programmers never intend to use.  (for those who came crawling from there c++ farms). Though you can instead use break and continue with labels, to hop from one place to another.

No comments:

Post a Comment