hi,can anybody give me understanding regarding RETURN,CONTINUE,BREAK keywords ....when they are used?how they are used? what are unlabeled statements and labeled statements... i need simple explaination of these keywords and their uses... thank you ..
Amit Y Desai
Ranch Hand
Joined: Dec 06, 2007
Posts: 35
posted
0
hi,can anybody give me understanding regarding RETURN,CONTINUE,BREAK keywords ....when they are used?how they are used? what are unlabeled statements and labeled statements... i need simple explaination of these keywords and their uses... thank you
consider while loop
This loop is to skip processing when i=5...continue is used to skip further processing for current loop and continue from begining of loop. -------------------------- consdier onother while loop
This code will start loop from i=10 to i=100..when i=100..it will come out of loop.. break statement is used in loop to break unconditionally out of the loop...
----------------- return statement is used to return from running funciton...it can return value along.
here int is the return type of getValue function...this function on calling returns value 5
Karthikeyan Ravindran
Ranch Hand
Joined: Nov 20, 2007
Posts: 32
posted
0
The break keyword is used to terminate a loop early. Consider the following example, which searches through an array for a particular value.
for (int i = 0; i < array.length; i++) { if (array[i].equals("yes")) { foundYes = true; break; } }
And continue does not terminate a loop. Rather, it skips to the next iteration of the loop, and stops executing any further statements in this iteration. This allows us to bypass the rest of the statements in the current sequence, without stopping the next iteration through the loop.
for (int i = 0; i < array.length; i++) { if (array[i] == null) // skip this one, goto next loop continue; else { // do something with array[i] ....... } }
karthikeyan
Regards,<br /> <br />karthikeyan<br /> <br />Life is Beautiful..<br />Enjoy every Moment
Karthikeyan Ravindran
Ranch Hand
Joined: Nov 20, 2007
Posts: 32
posted
0
And keyword return followed by an expression returns the value of the expression to the caller of the method.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Amit Y Desai:
consider while loop
Note that if i is less than or equal to 5 before this code is called it will cause an infinite loop as i will never become bigger than 5. Also the 'else' is not required.
[ February 20, 2008: Message edited by: Joanne Neal ]
Joanne
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
Except in two specific situations, frequent use of return, break and continue keywords is a hint that the programmer was not very good.
These keywords interrupt the usual flow of control, making it harder to work out what is going on. In contrast, good code is easy to understand and to maintain.
The two exceptions are: -
break is needed in switch statements
return is needed at the end of a non-void method
Other than these, you should think carefully whether there is a clearer alternative, before using return, continue or break.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P