LATEST UPDATES

Friday 18 November 2016

What is the difference between continue and break statement?



Break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.

In below example, Loop is broken when counter reaches 4.



for (counter=0;counter
System.out.println(counter);
 
if (counter==4) {
 
break;}
 
}



In the below example when counter reaches 4, loop jumps tonext iteration and any statements after the continue keyword are skipped for current iteration.



for (counter=0;counter
System.out.println(counter);
 
if (counter==4) {
 
continue;
 
}
 
System.outprintln("This will not get printed when counter is 4");
 
}
Java

1

for (counter=0;counter

No comments:

Post a Comment

Designed By Blogger Templates