using goto in nested loops

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

for (int serie=1; serie < TurnierGruppe.AktuelleSerie.Serie; serie++)
{
//outer: here is works
foreach (Tischbesetzung tb in
TurnierGruppe.Serien[serie].Tischebesetzungen)
{
for (int i=0; i<tb.SpielerAnzahl; i++)
{
if (tb.Spieler.Spieler==this)
{
gespielteSerien.Add(tb.Spieler);
goto outer;
}
}
}
outer:
}

Iam getting an error that a semicolon is missing after the label. If I move
the label to the top of the loop it works.
I guess C# doesn't allow labels with no code following directly, if so I'd
like to know the reason.

The problem for me is now if I'd move the label to the top of the loop the
enumerator will not be moved to the next element which would cause serious
trouble for me.
 
i know, this is not an answer, but why dont you do this (use flag & break):


bool flag = true;
 
i know, this is not an answer, but why dont you do this (use flag &
break):
bool flag = true;
for (int serie=1; serie < TurnierGruppe.AktuelleSerie.Serie && flag; serie++)
{
//outer: here is works
foreach (Tischbesetzung tb in
TurnierGruppe.Serien[serie].Tischebesetzungen)
{
for (int i=0; i<tb.SpielerAnzahl; i++)
{
if (tb.Spieler.Spieler==this)
{
gespielteSerien.Add(tb.Spieler);
flag = false; break;
}
}
}
outer:
}




Using flags degrades performance, is error prone and less readable than even
goto.
Now the question arises why C# doesn't allow labeled breaks like Java.
 
cody said:
[...]
Iam getting an error that a semicolon is missing after the label. If I move
the label to the top of the loop it works.
I guess C# doesn't allow labels with no code following directly, if so I'd
like to know the reason.

The problem for me is now if I'd move the label to the top of the loop the
enumerator will not be moved to the next element which would cause serious
trouble for me.

Just add a semi-colon after the colon:

outer:;

C# labels require a statement, so give it a blank statement.
 
Hi Cody,

A label needs to have some code it refers to.

Try putting an empty code block {} after outer:

outer: {}

Happy coding!
Morten Wennevik [C# MVP]
 
then add a semicolon. "outer: ;" if you don't like the look of it, do "outer: continue;" I don't like goto, never had to use it. but I guess using it to get yourself out of very deeply nested loops is not THAT bad

----- cody wrote: ----

for (int serie=1; serie < TurnierGruppe.AktuelleSerie.Serie; serie++

//outer: here is work
foreach (Tischbesetzung tb i
TurnierGruppe.Serien[serie].Tischebesetzungen

for (int i=0; i<tb.SpielerAnzahl; i++

if (tb.Spieler.Spieler==this

gespielteSerien.Add(tb.Spieler)
goto outer



outer


Iam getting an error that a semicolon is missing after the label. If I mov
the label to the top of the loop it works
I guess C# doesn't allow labels with no code following directly, if so I'
like to know the reason

The problem for me is now if I'd move the label to the top of the loop th
enumerator will not be moved to the next element which would cause seriou
trouble for me

-
cod

Freeware Tools, Games and Humou
http://www.deutronium.de.vu || http://www.deutronium.t
 
I don't like goto, never had to use it. but I guess using it to get
yourself out of very deeply nested loops is not THAT bad.


Yes, in this case a goto is the best solution. Iam still wondering why .NET
doesn't support labeled breaks like Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top