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 13 11250
I think there is no built-in language support for doing that. Again, you have
a work-around - use a Label and goto statement. :-)
"PeterZ" wrote: 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
> 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; } }
Peter,
In your case as presented I believe the following (caveat - untested code!) will
work (note the added braces around the "if" clause):
for (int i=0; i<=myArray.Leng th; i++)
{
if (myArray[i] == true)
{
switch (i)
{
case 0 :
... do somehting
break; // exit the switch
case 1 :
... do somehting
break; // exit the switch
}
break; // exit the for loop
}
}
HTH,
-rick-
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 PeterZ,
I would put the body of the for loop into another function to aid
readability. The function will return a boolean indicating if the for loop
should continue executing. So the following code:
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;
}
}
becomes:
public void FunctionLoop()
{
for (int i=0; i<=myArray.Leng th; i++)
{
if (myArray[i] == true)
if(!DoLoopWork( i))
{
//break out of the loop
break;
}
}
}
public bool DoLoopWork(int i)
{
bool blnContinueLoop ing = true;
switch (i)
{
case 0 :
... do somehting
break;
case 1 :
... do somehting
//exit the for loop;
blnContinueLoop ing = false;
break;
}
return blnContinueLoop ing;
}
Hope that helps
Mark R Dawson http://www.markdawson.org
"PeterZ" wrote: 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
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
Thanks guys!
I my particular case, I think Rick's is the most simple, but I can see where
on other occasions the other suggestions would be more appropriate - eg.
where you only wanted to exit the loop in a specific case statement rather
than all of them.
Thanks again!
PeterZ
"PeterZ" <_r************ **********@hotm ail.com> wrote in message
news:eB******** *****@tk2msftng p13.phx.gbl...
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
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
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
I agree, in your case Rick's solution is really the best.
However, if you want to exit the for loop in some cases but not in some
other, you could put the loop and switch inside a method and use the return
statement:
private void myFunction(bool[] myArray)
{
for (int i=0; i<myArray.Lengt h; i++)
{
Console.WriteLi ne(i);
if (myArray[i] == true)
switch (i)
{
case 0:
Console.WriteLi ne("Case 0");
return;
case 1:
Console.WriteLi ne("Case 1");
break;
default:
Console.WriteLi ne("Default");
break;
}
}
}
However, my opinion is that using break in a loop really is a jump statement
and should be used carefully.... you can always implement the desired
behaviour using flags and a while loop... most languages do not have a break
statement for terminating loops and this is how you implement that behaviour
in them. By naming the flag with a meaningful name your code will be more
readable, using break may not always be so clear, for instance if you have
nested loops. In other words, I prefer Oliver's solution over mine above.
"PeterZ" wrote: Thanks guys!
I my particular case, I think Rick's is the most simple, but I can see where on other occasions the other suggestions would be more appropriate - eg. where you only wanted to exit the loop in a specific case statement rather than all of them.
Thanks again! PeterZ
"PeterZ" <_r************ **********@hotm ail.com> wrote in message news:eB******** *****@tk2msftng p13.phx.gbl... 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 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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |