Hi,
Back to basics!
My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Leng th; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;
case 1 :
... do somehting
exit the for loop;
case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ
Nov 17 '05
13 11252
Just for saving your time: The following code compiles well, but it is a
U-turn:
for(int i = 0; i<10; i++) {
switch( i ) {
case 0: Console.WriteLi ne("Start"); break;
case 4: {
Console.WriteLi ne("Haf");
break;
};
break;
default: Console.WriteLi ne(i);break;
}
}
Console.WriteLi ne("End");
Produces funny:
Start
1
2
3
Haf
5
6
7
8
9
End
Perhaps there should be some way of naming blocks of code, so one might
specify to which the break statment applies.
"jgorlick" wrote: I agree that goto is not ideal, that it even is poor design.
The fact that there continues to be a case when goto is often used means that it is poor language design. In this specific case, C# does not provide a way to exit the for (break) without confusing this with the exit case (also break).
I would suggest that a nested function ala Pascal would provide the ability to break from the for within the switch. The nested function is the ideal solution as the function is only intended to be called from within this function.
While C# doesn't provide nested functions, use a function and figure a way to not pollute the namespace with this function yourself.
"Michael S" wrote:
I think Oliver hit the mark. This is how Iwould do it.
And I would never let a goto go live if there was another solution. And each time I see a goto I start to think. And always I solve them. A goto is the same as poor design.
Happy Spaghetti - Michael S
"Oliver Sturm" <ol****@sturmne t.org> wrote in message news:xn******** ********@msnews .microsoft.com. .. PeterZ wrote:
>My understanding is that the only way to exit a For-Next loop prematurely >is >with the 'break' keyword. How are you supposed to do that if you're >inside >a Switch statement? The break keyword will only come out of the switch, >not >the for-next loop.
Well, this is really one good example why break isn't much better that goto. Why don't you just set a flag:
bool endLoop = false; for (int i = 0; i <= myArray.Length && !endLoop; i++) { ... switch(i) { case 0: // whatever endLoop = true; break; } }
This way you have a clearly defined loop, you can find out after the loop why exactly you left it, and you don't go jumping around the program flow like break does.
I'm not saying never use it - but I do think it should be used only in the simplest situations where it's absolutely clear what will happen.
Oliver Sturm -- omnibus ex nihilo ducendis sufficit unum Spaces inserted to prevent google email destruction: MSN oliver @ sturmnet.org Jabber sturm @ amessage.de ICQ 27142619 http://www.sturmnet.org/blog
hi,
the way i've exited for loops in the past is to set the loop variable
to the max so the loop drops out naturally on it's next itteration.
i.e.
for (int i=0; i<=myArray.Leng th; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
i = myArray.Length + 1;
break;
case 1 :
... do somehting
i = myArray.Length + 1;
break;
case 2 :
... do somehting
i = myArray.Length + 1;
break;
}
}
i've never had any problems with this method.
hope this helps
:)
i do this too, there is convention that states that code should manipulate
the counter. if you don't care about convention, that's fine
--
Regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
"raican" <go**********@r aican.co.uk> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . hi,
the way i've exited for loops in the past is to set the loop variable to the max so the loop drops out naturally on it's next itteration.
i.e. for (int i=0; i<=myArray.Leng th; i++) { if (myArray[i] == true) switch (i) { case 0 : ... do somehting i = myArray.Length + 1; break; case 1 : ... do somehting i = myArray.Length + 1; break; case 2 : ... do somehting i = myArray.Length + 1; break; } }
i've never had any problems with this method.
hope this helps :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: QQ |
last post by:
I know there are many functions that I can exit the program such as
return 0,
exit(0), exit(1),_EXIT(0) ....
What are the difference between them?
Thanks a lot!
|
by: Brendan Miller |
last post by:
I am trying to close my application using Application.exit() in the
frmMain_Closing event. When the form closes the process does not. My
application only has one form (no other classes either). The form has
a really long loop which generates approx. 700 .csv files. I do not create
any threads myself (Application.exitthread() doesn't work either).
To counteract this I have decided to use the Environment.exit(-1)
method instead.
What...
|
by: Ioannis Vranos |
last post by:
I am currently reading a chapter involving multithreading, and some sample code calls
Environment::Exit() to terminate the application with all threads.
What is the difference from Application::Exit()? Does Application::Exit() terminate only
the main thread and background threads, and not the foreground threads?
|
by: Bob Day |
last post by:
Using VS 2003, VB.net...
I am confused about the Application.Exit method, where the help states "This
method does not force the application to exit." Aside from the naming
confusion, how do I force the application to exit? See **** below for my
comments/questions in a simple example.
****In Sub Main, the Application.Exit works as expected, other sub main code
is ignored, and when the end sub is reached the application shuts down....
|
by: Zeno Lee |
last post by:
What is the best way to return an exit code from a VB.NET windows forms app?
My Forms application is dual purpose. It is an interactive windows app. It
is also automated and run via a script and it needs to return an exit code
to that script.
I've tried a few ways to return an exit code.
1. As a forms app with Entry point being Form1, I've tried
Application.Exit(999). It does not return the exit code.
| |
by: Atanas Banov |
last post by:
i ran onto this weirdness today: seems like close() on popen-ed
(pseudo)file fails miserably with exception instead of returning exit
code, when said exit code is -1.
here is the simplest example (under Windows):
>>> print popen('exit 1').close()
1
>>> print popen('exit -1').close()
Traceback (most recent call last):
|
by: Vicky |
last post by:
Please tell me at
vdkhakhkhar@gmail.com
|
by: yawnmoth |
last post by:
To quote from <http://php.net/function.include>,
"Because include() is a special language construct, parentheses are
not needed around its argument. Take care when comparing return
value."
exit is a language construct, also. To quote from <http://php.net/
function.exit>,
"this is a language construct"
|
by: mathieu |
last post by:
Hi there,
I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. I would like to
reuse it as a library. For that I renamed the 'main' function into
'mymain', but I am stuck as to what I should do for the 'exit'.
AFAIK there is no portable way to catch the exit. The only thing I
can think of is atexit, but that does not work since 'exit' is still
called afterward.
|
by: Gary Robinson |
last post by:
In Python 2.5.2, I notice that, in the interpreter or in a script, I can exit with:
exit()
But I don't see exit() mentioned as a built-in function; rather the Python Library Reference says we should use sys.exit(). Also, the reference says sys.exit() is like raising SystemExit. But so is just calling exit(). For instance,
exit('some error message')
has the same apparent effect as
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |