473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

using goto in nested loops

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[i].Spieler==this)
{
gespielteSerien.Add(tb.Spieler[i]);
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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #1
9 1851
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[i].Spieler==this)
{
gespielteSerien.Add(tb.Spieler[i]);
flag = false; break; }
}
}
outer:
}


Nov 16 '05 #2
> 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[i].Spieler==this)
{
gespielteSerien.Add(tb.Spieler[i]);
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

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #3
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]
Nov 16 '05 #4
cody wrote:
[...]
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.
Nov 16 '05 #5
> Just add a semi-colon after the colon:

outer:;

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


How funny :)

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #6
Even simpler than an empty code block :)

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7

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[i].Spieler==this

gespielteSerien.Add(tb.Spieler[i])
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

Nov 16 '05 #8
Try to change "outer:" to "outer:;".

"cody" <no****************@gmx.net> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
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[i].Spieler==this)
{
gespielteSerien.Add(tb.Spieler[i]);
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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

Nov 16 '05 #9
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Samee Zahur | last post by:
In a recent thread, I saw the topic of goto coming up, so here's a question: With a background of asm langs, I know exactly why goto is to be avoided. But does anyone know of a good way for...
39
by: vineoff | last post by:
If I'm having nested loops like: for (...) for (..) for (...) { /* exit here */ } and I need to exit from there ^ . Is it better to use exceptions or goto or some other method?
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
22
by: Nimmi Srivastav | last post by:
Can someone kindly clarify the distinction between long jumps and gotos? Why is one frowned upon and not the other? Is there really any situation where use of longjmp becomes inevitable? A...
28
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use...
13
by: Kartic | last post by:
Hi, Is it good practice using GOTO in .NET application? Please advice. Thanks, Kartic
3
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
59
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.