Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while: wend
#1
On Terry Ritchie's function/subroutine part of the QB64 tutorial, line 15 of subs.base,  we have:

Code: (Select All)
WHILE _MOUSEINPUT: WEND

The only way I've seen "WHILE" and "WEND" used is at the start and end of a loop, with some instructions  in between, which then ends with 'WEND".
I'm confused on what is exactly going on here.  Can anyone explain like I'm a five year old?

Code: (Select All)
DO
    _LIMIT 60
    WHILE _MOUSEINPUT: WEND
    IF _MOUSEBUTTON(1) THEN DrawStar _MOUSEX, _MOUSEY
    IF _MOUSEBUTTON(2) THEN Directions
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)
Reply
#2
WHILE _MOUSEINPUT: WEND


While....  there's input in the mouse update buffer
Repeat

It basically clears and updates the mouse buffers so you're getting the current mouse status rather than some backlog of past X/Y positions which doesn't mean anything to you anymore.
Reply
#3
"WHILE... WEND" was the only provided way by GW-BASIC to do loops without relying on "GOSUB/RETURN" and "GOTO".

"DO... LOOP" is superior, I had first seen it in Commodore 128 BASIC, in "RUN" magazine. This game I wanted badly on my Tandy1000HX LOL back in the late 1980's. Some guy wrote a book about programming in QuickBASIC, said "WHILE... WEND" was acceptable and otherwise "GOTO" with clear labels, and "DO... LOOP" was too much like Pascal and he didn't like it. To try to make the older construct more useful "EXIT WHILE" was added. That's why now we also have "EXIT SELECT" which is sloppy also.

QuickBASIC didn't have "EXIT WHILE" nor "_CONTINUE", and therefore the only way to create a "run-once" loop was to employ a "DO... LOOP" that purposely fell through at the end such as "LOOP WHILE 0" or "LOOP UNTIL 1". The idea was to be able to use "EXIT DO" to skip a bunch of lines within the false loop according to a condition. See the Wiki example for "DO... LOOP" which I think was one of the most useful to me.

https://qb64phoenix.com/qb64wiki/index.php/DO...LOOP

(Example #3)
Reply
#4
"WHILE... WEND" was the only provided way by GW-BASIC to do loops without relying on "GOSUB/RETURN" and "GOTO".

Nope! For... Next was in there first even before While... Wend

Dartmouth BASIC - Wikipededia
Quote:List of BASIC statements
DEF
define single line functions
DIM
(short for dimension) define the size of arrays
END
define the end of the program
STOP
stop a program before the textual end
FOR / TO / STEP
define loops
NEXT
mark the end of loops
GOSUB
transfer control to simple subroutines
RETURN
return control from simple subroutines
GOTO
transfer control to another statement
IF / THEN
decision making
LET / =
assign formula results to a variable
PRINT
output results
DATA
store static data within the program
READ
input data stored in DATA statements
REM
comment ("REMark")
b = b + ...
Reply
#5
This use to be THE WAY to pause a program waiting for a key press.
Code: (Select All)
Print "We will pause until you press some key..."
While Len(InKey$) = 0: Wend
Print "Hello World!"


Now days there are better ways unless you want to blow the dust off your CPU fan.
b = b + ...
Reply
#6
"ELI5" attempt:


I was confused by this as well.   WHILE _MOUSEINPUT: WEND looked like a single line of code to me.    The : symbol is used to separate lines of code on the same line. 

An example would be...

FOR t = 1 TO 10 : NEXT t


If you forget that and just think of it as two lines...

WHILE _MOUSEINPUT
WEND

Then you can just consider reply #2.   But that was also confusing to me a while ago.   I wondered how this doesn't just loop infinitely.
But it just goes and checks for the latest mouse input and then exits the loop.
Reply
#7
(01-03-2023, 03:39 PM)bplus Wrote: "WHILE... WEND" was the only provided way by GW-BASIC to do loops without relying on "GOSUB/RETURN" and "GOTO".

Nope! For... Next was in there first even before While... Wend

Nobody was going to use a "FOR... NEXT" loop to create what looked like an infinite loop. I stand corrected, because some people forced a much longer loop than was set in "FOR" statement by simply setting the loop variable so it never reached the destination...

This creates an endless loop:
Code: (Select All)
FOR t = 1 to 10
t = 1
'rest of loop code
NEXT t

Also "FOR... NEXT" always required a variable, although breaking out of an endless "WHILE... WEND" needed to test something. Anyway I was trying to emphasize that "DO... LOOP" was presented later as an alternative away from 8-bit computers having BASIC interpreters.
Reply
#8
If "wend" one go down, "while" can not go up! -- Where is the problem?  Tongue
Reply
#9
just right

Code: (Select All)
10 Rem while
If _MouseInput Then 10
Rem wend
Reply
#10
Here's how to do _MouseInput without While...Wend but just a GOTO. A one liner like While _MouseInput :Wend and without double parking (2 statements separated with colon).

Code: (Select All)
_Title "Click to draw paths from one click to next" 'b+ 2023-01-03

Do
    10 If Not _MouseInput Then GoTo 10
    mx = _MouseX: my = _MouseY: mb = _MouseButton(1)
    If mb Then
        If lastx <> 0 And lasty <> 0 Then
            Locate my, mx: Print "X";
            If mx > lastx Then
                While lastx < mx
                    lastx = lastx + 1
                    Locate lasty, lastx: Print "X";
                Wend
            Else
                While lastx > mx
                    lastx = lastx - 1
                    Locate lasty, lastx: Print "X";
                Wend
            End If
            If my > lasty Then
                While lasty < my
                    lasty = lasty + 1
                    Locate lasty, lastx: Print "X";
                Wend
            Else
                While lasty > my
                    lasty = lasty - 1
                    Locate lasty, lastx: Print "X";
                Wend
            End If
        End If
        lastx = mx: lasty = my
    End If
Loop

EDIT: No sooner had I posted this when I realized the 2nd GOTO was not needed. Cool!

Looks like TempodiBasic was kinda close ;-))
b = b + ...
Reply




Users browsing this thread: 4 Guest(s)