Thursday, 21 April 2011

Continue, Break and Return

Continue statements are used in looping statements to force another iteration of the loop before reaching the end of the current one. Most often continue is used as part of a conditional statement that only happens in certain cases. The following is a trivial example.
int x=0;
while (x<10)
{
x++;
System.out.println(x);
continue;
// you will never get to this point!!
};
Break statements are used in looping and 'switch' statements to force an abrupt termination or exit from the loop or switch. In the following example the loop is never completed. Once again the normal use of break is as part of a conditional statement.
int x=0;
while (x<10)
{
x++;
System.out.println(x);
break;
// you will never get to this point!
};
Return statements are used to force a quick exit from a method. They are also used to pass values back from methods.

No comments:

Post a Comment