Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

For loops...what;s the point in them???

What is the point of a FOR loop? Several programmers have tried (unsuccessfully) to teach me the purpose of a for loop.....why can't IF loops just be used?? I can't really see the difference....Please email me!!


Comments

  • : What is the point of a FOR loop? Several programmers have tried (unsuccessfully) to teach me the purpose of a for loop.....why can't IF loops just be used?? I can't really see the difference....Please email me!!


    what you probably mean by an IF loop is

    start:

    ... CODE ...

    IF NOT(FOOBAR = 100) THEN FOOBAR=FOOBAR+1; GOTO start


    Am I right ?

    If yes, then i well tell you than a FOR loop just

    makes your code much clearer, slightly faster because

    in this situation a FOR loop is the obvious choice,

    simply because thats what its for.

    If you have imbricated loops, you just have to use for

    unless you wand your code illegible. See example:


    FOR I=1 TO 100 STEP 5

    FOR J=1 TO 100 STEP 5

    FOR K=0 TO 20 STEP .2

    ... CODE ...

    NEXT K

    NEXT J

    NEXT I


    is simpler, clearer, faster, more logical, better than


    I = 0: J = 0: K = 0

    start_loop: 'Endless loop

    ... CODE ...

    IF NOT(K=20) THEN K = K + .2: GOTO start_loop

    IF NOT(J=100) THEN J = J + 5: GOTO start_loop

    IF NOT(I=100) THEN I = I + 5: GOTO start_loop


    Its just a matter of clarity

    Get a FOR loop and everyone will understant that

    you have three imbricated loops

    The IF loop with GOTOs are apalling.

    GET RID OF GOTOs !

    ITS POSSIBLE !

    HAVE NONE IN YOUR CODE, THEY REALLY SUK


    Got it ?




  • No text == *nt* =)




  • Hi there...


    Well in general people say that GOTO's are bad

    for you, and yes I agree.. in most situations

    you can get by without using a GOTO statement..


    You can use stuff like:

    FOR, NEXT: DO, LOOP: WHILE, WEND...




    BUT =) IMO there are some cases where a goto perhaps is not a very bad idea to use... like:


    for x = 0 to 100

    for y = 0 to 100

    for z = 0 to 100

    if condition = TRUE then goto OUTofLOOP

    next

    next

    next

    OUTofLOOP:




    You have a lot of nestled loops, and you want to get out from them all..

    But yes I know you can get by using a while, wend, or do, loop......

    But I just wanted to mention it.


    I try to stay away from GOTO's as much as I possibly can..

    It was a LONG time since I used one.




    Ya, if you have anything to comment please do..

    Bye fellow programmes!






  • Hi there...


    Well in general people say that GOTO's are bad

    for you, and yes I agree.. in most situations

    you can get by without using a GOTO statement..


    You can use stuff like:

    FOR, NEXT: DO, LOOP: WHILE, WEND...




    BUT =) IMO there are some cases where a goto perhaps is not a very bad idea to use... like:


    for x = 0 to 100

    for y = 0 to 100

    for z = 0 to 100

    if condition = TRUE then goto OUTofLOOP

    next

    next

    next

    OUTofLOOP:




    You have a lot of nestled loops, and you want to get out from them all..

    But yes I know you can get by using a while, wend, or do, loop......

    But I just wanted to mention it.


    I try to stay away from GOTO's as much as I possibly can..

    It was a LONG time since I used one.




    Ya, if you have anything to comment please do..

    Bye fellow programmes!






Sign In or Register to comment.