what loop???

This is a discussion on what loop??? within the Game Programming forums, part of the General Programming Boards category; I have a strange question, does the WHILE loop work the same as the goto loop did in BASIC?...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    what loop???

    I have a strange question, does the WHILE loop work the same as the goto loop did in BASIC?

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    While loops come in a few forms:

    Code:
    //Do everything in the loop
    //If our condition is not met, restart the loop
    //(Will print X from 0 to 9)
    
    int X = 0;
    do
    {
    	printf("%d", X);
    	X++;
    }while(X != 10);
    
    
    //Check if our condition is true
    //If not, do everything in the loop
    //Then jump back to our condition and check again
    //before processing the loop
    //(Will print X from 0 to 9)
    
    int X = 0;
    while(X != 10)
    {
    	printf("%d", X);
    	X++;
    }
    Note, the difference in style between the two occurs (do-while loop/while loop) when you absolutley always want the loop to execute atleast once. I.e.
    Code:
    //Will execute the contents of the loop once
    int X = 0;
    do
    {
    	printf("%d", X);
    }while(X != 0);
    
    //Will NOT execute the contents of the loop
    int X = 0;
    while(X != 0)
    {
    	printf("%d", X);
    }
    Now, I'm not sure what a goto loop in BASIC acted like, but you can compare that to BASIC and make the decision for yourself probably.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    I think what's meant by a BASIC loop is:

    Code:
    x = 0
    
    label FOO
    x = x + 1
    if x < 10
    { goto FOO }
    I don't know much about BASIC though.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well that's extremely old BASIC.

    A QBasic while loop is not thay much different than a C while loop.

    QBasic 4.0,4.5, and 7.1 PDS
    Code:
    
    dim x as integer
    
    'While/Wend loop
    while (x<100)
      x=x+1
    wend
    
    x=0
    
    'Do while loop
    do while (x<100)
      x=x+1
    loop
    
    x=0
    
    'do loop while loop
    
    do
      x=x+1
    loop while x<100
    GOTO loops are not considered good style in QBasic. QBasic is modular and somewhat object oriented (not totally - it is still an unstructured language for the most part) and the need for GOTO has been since eliminated throughout the language, except in error handling. It's error handling mechanism uses GOTO, however the keyword GOSUB is even more outdated than GOTO.

    Code:
    sub OpenFile(filename as string)
    on local error goto LocalErrorHandler
    
    dim ch as string *1
    open filename for input as #1
    input #1,ch
    close #1
    
    exit sub
    
    LocalErrorHandler:
    
    select case ERR
      case 53:
         print "Cannot open "filename
      case 57:
         print "Bad file mode"
      case else:
         print "File error: ";ERR
    end select
    exit sub
    
    end sub
    A GOTO loop in QBasic would look like this:
    Code:
    dim x as integer
    x=0
    
    Loop:
      x=x+1
    if (x<100) then goto Loop
    Note this is very similar to an assembly loop based on the CX register count which would look somewhat like this:

    Code:
    xor ax,ax
    mov cx,100
    
    StartOfLoop:
      inc  ax
      dec cx
      LOOP StartOfLoop
    Or an assembly loop using the cmp/j(x) instruction sequence.

    Code:
    xor ax,ax
    
    StartOfLoop:
      ;increment ax
      inc ax
      ;if ax<100 goto StartOfLoop
      cmp ax,100
      jb StartOfLoop

    Most loops in any computer language behave in somewhat the same way. C is no different. The only thing you must understand is when the loop exits and when the condition(s) for exiting is/are checked so that you exit at the correct time.
    Last edited by VirtualAce; 06-08-2005 at 08:06 AM.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Bubba, I'm suprised you remember so much QBasic!

    With this assembly snippet
    Code:
    xor ax,ax
    mov cx,100
    
    StartOfLoop:
      inc  ax
      dec cx
      LOOP StartOfLoop
    I don't understand how this would work? Wouldn't this be an infinite loop because you don't compare any registers? And what is LOOP? I assume it's a macro of some kind?

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    LOOP is not a macro. It's a valid Intel x86 instruction. The actual definition is 'Loop according to ECX counter'. Basically it will loop to the specified label as long as (E)CX !=0.

    However there is a bug in the code because the loop instruction automatically decrements (E)CX so that loop will actualy only loop 50 times, and not 100.

    The form I used is this:

    Opcode: E2 cb
    Instruction: LOOP rel8
    Description: Decrement count; jump short if count !=0

    So actually this is the correct asm code to loop 100 times and track in (E)AX the number of iterations.

    Code:
    xor ax,ax
    mov cx,100
    
    StartOfLoop:
      inc ax
      loop StartOfLoop

    Consult the Intel IA32 Architecture Software Developer's Manual, Volume 2: Instruction Set Reference for more information.

    And as for QBasic, that code I posted will also work in Visual Basic 6, except for inside of the error routine you may want to call the API and pop up a Message Box displaying the error - and I think Bad file mode is actually ERR number 56, not 57.
    Last edited by VirtualAce; 06-09-2005 at 08:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21