Exit from for loop

ritesh2190

Member
Joined
Jun 7, 2011
Messages
15
Programming Experience
1-3
VB.NET:
For Each line1 In TextBox1.Lines
                            sample1 = TextBox1.Lines(k3)
                            Dim len1 As String
                            len1 = sample.Length
                            ' For y6 = 0 To len1 - 1
                            y7 = InStr(sample1, "=-" + fg)
                            If y7 <> 0 Then
                                TextBox2.Text = TextBox2.Text + vbCrLf + " rule 39 violated at line " + k3.ToString()
                            End If
                            'Next
                            k3 = k3 + 1
                            If k3 = TextBox1.Lines.Length - 1 Then
                                Exit For
                            End If
                        Next

in the above code wen k3 is equal to the length of the textbox still the for loop is running again it is not coming out of it.
pls help!!!!!!!
 
That's a bit of a silly loop. The whole point of using a For Each loop is that you do something for each item in a list. If you want to count the lines then you should use a For loop, not a For Each loop. Here's an example that contrasts the use of the two:
Dim lines As String() = New String() {"line1", "line2", "line3"}

For Each line As String In lines
    MessageBox.Show(line, "For Each")
Next

For index As Integer = 0 To lines.GetUpperBound(0)
    Dim line As String = lines(index)

    MessageBox.Show(line, "For")
Next
 
That's a bit of a silly loop. The whole point of using a For Each loop is that you do something for each item in a list. If you want to count the lines then you should use a For loop, not a For Each loop. Here's an example that contrasts the use of the two:
Dim lines As String() = New String() {"line1", "line2", "line3"}

For Each line As String In lines
    MessageBox.Show(line, "For Each")
Next

For index As Integer = 0 To lines.GetUpperBound(0)
    Dim line As String = lines(index)

    MessageBox.Show(line, "For")
Next
VB.NET:
 For k3 = 0 To TextBox1.Lines.Length - 1
                            sample1 = TextBox1.Lines(k3)
                            Dim len1 As String
                            len1 = sample.Length
                            y7 = InStr(sample1, "=-" + fg)
                            If y7 <> 0 Then
                                TextBox2.Text = TextBox2.Text + vbCrLf + " rule 39 violated at line " + k3.ToString()
                                Exit For
                                y1 = 0
                            End If
                            k3 = k3 + 1
                        Next
as u told i have changed my code from the for each loop to for loop but still the exit for statement is not making the control go out of the loop!
pls help!!!!!
 
Assuming that there is no system corruption, if an Exit For statement is hit, the For loop will exit, plain and simple.

Maybe you could explain what you're actually trying to achieve instead of showing us some code that doesn't work and expecting us to work it out for ourselves, which is pretty hard given that there are all sorts of obvious issues with that code and, I'm sure, some not so obvious. If you want us to help then don't keep important information like what the code is actually supposed to do a secret from us.
 
VB.NET:
 'check for rule 9
        'k = 0
        'k1 = 0
        'For Each line In TextBox1.Lines
        'sample = TextBox1.Lines(k)
        'Dim len As String
        'len = sample.Length
        'For i = 0 To len - 1
        '   r = InStr(sample, "/*")
        '    If r <> 0 Then
        '         r = InStr(r + 1, sample, "/*")
        '      End If
        '       If r <> 0 Then
        '            TextBox2.Text = TextBox2.Text + vbCrLf + "rule 9 is violated at line" + k.ToString()
        '         End If
        '          If r = 0 Then
        '               Exit For
        '            End If
        '         Next
        '          k1 = k
        '           For Each line1 In TextBox1.Lines
        '                sample = TextBox1.Lines(k1)

        'Dim len1 As String
        'len1 = sample.Length
        'For j = 0 To len1 - 1
        '    r = InStr(sample, "/*")
        '   If r <> 0 Then
        '      TextBox2.Text = TextBox2.Text + vbCrLf + "rule 9 is violated at line" + k1.ToString()
        '     r = InStr(sample, "*/")

        '  End If
        '   Next
        '      k1 = k1 + 1
        ' Next

        ' Next
        ' k = k + 1
        ' Next
actually i am developnig a vb.net application which takes a c program as input and checks it for Misra c guidelines,identifies any deviations and displays them
in the above code i am checking for nesting of comments
 
The code in posts #1 and #2 is not checking for comments. Please explain what the code you're asking for help with in this thread is supposed to do. It's your code so you know what it's supposed to do. Just tell us.
 
You don't need to post anything, you have to debug your code. Put in breakpoints to see what variable values are at a given time during execution. If this is too time consuming due to amount of data processed you can instead output debug information that you go through and analyze afterwards, output the relevant information to help yourself pinpoint what the problem may be. If you just put a breakpoint on the Exit For statement you will see that it is called or not. If it is not called you have to debug the If condition where you want to exit For. Since you expect this condition to be met, and apparently it is not, you have to figure out the reason why.
 
What do you need that exit for command anyway? Surely it's better to just use a for loop as per jmcilhinney's first post.

If I've understood you correctly, this should do what you're after.
I assume that sample1, Y7 and fg are declared elsewhere in your code.

For K3 = 1 To TextBox1.Lines.Count ' Works through every line
               sample1 = TextBox1.Lines(K3 - 1) ' Gets the text of the line. Note that the line array is zero based, hence line one is lines(0)
               Y7 = InStr(sample1, "=-" + fg) ' Searches for the error you want it to find.
               If Not Y7 = 0 Then ' Gives the error message for rule 39 if the mistake is found
                 TextBox2.Text = TextBox2.Text + vbCrLf + " rule 39 violated at line " + K3.ToString()
               End If
        Next ' Goes to the next line. It will automatically stop when it runs out of lines.
 
Back
Top