?
Solved

Why is this if-statement not breaking out of the for-loop?

Posted on 2007-07-23
3
Medium Priority
?
165 Views
Last Modified: 2010-04-15
I have an if-statement that I want to break out of the for-loop, but it's breaking out of the whole try clause. Here is the C# code I'm testing:

        try
        {
            foreach (string item in myArr)
            {
                if (item == "test")
                    break;
            }
         }
         catch {}

I'm trying to break out of the for-lop if the condition matches to do the next item. I would imaging that the if-break would break out of the for-loop. But when I stepped thru it in debugger, it's actually breaking the whole try statement. What am I missing here?
0
Comment
Question by:bemara57
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 1000 total points
ID: 19553300
There is no other code in the tryt block, so it's going to the next line which is outside of the try/catch.

Put a line of code after the foreach loop but in the try block - it will be executed.
0
 
LVL 10

Assisted Solution

by:SystemExpert
SystemExpert earned 1000 total points
ID: 19553322
It working fine
you have no statement after for loop

just add one statement after for loop and test it will come to that point

i.e
try
        {
            foreach (string item in myArr)
            {
                if (item == "test")
                    break;
            }
            int j=0;
            j+=1;
         }
         catch {}
0
 

Author Comment

by:bemara57
ID: 19553518
ahh, I was confusing break with continue. Thanks for pointing me to that direciton!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question