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

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?
bemara57Asked:
Who is Participating?
 
drichardsCommented:
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
 
SystemExpertCommented:
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
 
bemara57Author Commented:
ahh, I was confusing break with continue. Thanks for pointing me to that direciton!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.