Is it really so bad?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Is it really so bad?

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Is it really so bad?

    [Originally posted by KilltheSand]

    I'm converting some code into Visual Basic and I read that it was a terrible horrible thing if you modify the conditional variable of a for loop from within the loop. Is this true? I have an if within the loop and if it's true I do stuff, then I want to start the loop over. I would normally just set the variable back to zero before the end if. Is there any problem with this? As long as it's not true ever through the whole pass, the loop will terminate. I have some stuff to do after the loop so I don't want to use any recursion. Thank you for your comments.
    Steve O.

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Is it really so bad?

    [Originally posted by Greg DeBacker]

    I'm not sure if it will cause real problems in your program but it is bad programming. Some one would really have to think about how this affects the stack and I'm too tired to do that right now. It would be best to put the For...Next loop in a Do...Loop.

    Do Until bExitOK
    For k = 1 to 100
    If Something(k) = 74 Then
    Exit For
    Else
    bExitOk
    Exit For
    End If
    Next
    Loop

    Greg

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Is it really so bad?

    [Originally posted by FreeVBCode.com]

    In general it is not good practice but these types of things are only guidelines, not commandments. No need to be so dogmatic about it in my opinion.

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Is it really so bad?

    [Originally posted by Chris Carta]

    Yes, It's a very bad habit because it DOESN'T work.

    Maybe I'm being picky, but you mentioned a 'FOR LOOP'.

    In VB, that means only one thing. A block of code beginning with the key word FOR.

    The following will NOT exit the for/loop when endIdx = 7 as you might expect. Try it.

    Dim begIdx As Integer
    Dim endIdx As Integer

    endIdx = 10

    For begIdx = 0 To endIdx
    If begIdx = 5 Then
    endIdx = 7
    End If
    Next begIdx

    MsgBox "Last index iterated was " & begIdx - 1


    Instead, it loops until endIdx = 10, it's initial value which was set before the for/loop began. Setting endIdx within the loop doesn't do a thing to the for/loop once it is started.
    I hope this helps explain what the author of what you read was referring to.

    Chris

  5. #5
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Is it really so bad?

    [Originally posted by KilltheSand]

    Thanks for your help. Actually, I was changing the counting variable and not the destination variable but it is good to know the destination cannot be changed. Well, my function works fine (and I'm lazy) so I'm gonna leave it as is for now. Once I get my program finished I'll go back to optimize some code and find out if I'd rather modify variables or run two differant loops.
    Steve O.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center