# what loop???

• 05-30-2005
swgh
what loop???
I have a strange question, does the WHILE loop work the same as the goto loop did in BASIC?
• 05-30-2005
Epo
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.
• 06-07-2005
Nazca
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.
• 06-08-2005
VirtualAce
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.
• 06-08-2005
sand_man
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?
• 06-09-2005
VirtualAce
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.