Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return,continue,break keywords

 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

conside method of java


here int is the return type of getValue function...this function on calling returns value 5
 
Karthikeyan Ravindran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karthikeyan Ravindran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And keyword return followed by an expression returns the value of the expression to the caller of the method.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic