difference between continue and return

difference between continue and return

Postby joppiesaus » 29 Aug 2013, 09:43

Hi, I have a question:
You can use return to simply abort your command, but can you also use continue?
For example:
Code: Select all
if (p == null)
{
   //return or continue?
}


I know, if you do continue in a for loop, the for loop will be executed the next time. example:
Code: Select all
for (int i = 0; i < 10; i++)
{
   if (i == 0)
      continue; //only 0 will not be printed on the console. If you use break instead of continue, the for loop will be cancelled instead.
   Console.Write(i);
}
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: difference between continue and return

Postby qpqpqp123 » 04 Sep 2013, 23:03

you can do on
Code: Select all
if (p == null)
{
   //return or continue?
}

what you want
qpqpqp123
 
Posts: 59
Joined: 29 Apr 2013, 12:51

Re: difference between continue and return

Postby dzienny » 25 Sep 2013, 11:13

From C# Language Specification:
The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach statement.
The target of a continue statement is the end point of the embedded statement of the nearest enclosing while, do, for, or foreach statement. If a continue statement is not enclosed by a while, do, for, or foreach statement, a compile-time error occurs.
When multiple while, do, for, or foreach statements are nested within each other, a continue statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement must be used.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: difference between continue and return

Postby joppiesaus » 26 Sep 2013, 19:07

dzienny wrote:From C# Language Specification:
The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach statement.
The target of a continue statement is the end point of the embedded statement of the nearest enclosing while, do, for, or foreach statement. If a continue statement is not enclosed by a while, do, for, or foreach statement, a compile-time error occurs.
When multiple while, do, for, or foreach statements are nested within each other, a continue statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement must be used.

Wow! Thanks! :)
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron