TIPCORNER - SCAN VS WAIT

© 2002 by Alyce Watson

Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index


There is often confusion regarding the use of SCAN and WAIT. Both commands allow the program to check for user input, but there is a difference:

SCAN will poll the system for user events, THEN CONTINUE PROGRAM EXECUTION

WAIT will poll the system for user events, THEN STOP PROGRAM EXECUTION

"Input aVar$" is exactly equal to WAIT. The WAIT command was added because it seemed more descriptive of what was happening, and is the preferred command to use for that reason.

Here are some snips to illustrate. In this snip, since SCAN is used, the output screen will show both printouts:

'snip 1
print "one"
scan
print "two"

The program prints "one", scans for user events, then continues to print "two" Output in mainwindow will be:

one

two

In the next example, the program prints "one", then stops program execution and waits for user events. (Or perhaps a timer event.) The word "two" is never printed, because program exection stops at the WAIT command. Execution will continue later at a branch label associated with a button, menu item, or the timer, etc. Again, the "two" will NEVER be printed:

'snip 2
print "one"
wait
print "two"

The third example is exactly the same as the second. It simply uses "input a$" instead of "WAIT". The two commands may be used interchangeably. The program NEVER prints "two" because execution stops at the "input a$" statement, and will later continue at a branch label associated with a control, or perhaps at a timer event handler.

'snip 3
print "one"
input a$
print "two"

USING SCAN TO CYCLE CONTINUOUSLY

If there is a need for a program to perform repetitive actions, and check for events constantly at the same time, then SCAN should be used in the cycling loop. It might look like this:

[CycleLoop]
    num = int(rnd(0)*2000)
    print  num
    SCAN
    goto [CycleLoop]

The routine above didn't really accomplish much. It was just meant to show that actions can be performed continuously in a loop. The cycle loop can contain many lines of code that perform many actions, including GOSUBS, and calls to SUBS and FUNCTIONS. To see why SCAN is necessary in the routine above, remove it and try the snippet again. You'll find that you cannot stop the program from running! In that instance, press the "ctrl" key, hold it down and hit the "break" key.

Carl Gundel says, "Scanning is an appropriate technique when you want a loop to run as fast as the computer can execute it (using WAIT and a timer will run the same speed on all computers as long as they are fast enough to run all code up to the WAIT before each timer tick)."

Here is the same routine as above, but run on a timer rather than in a continuous loop. It will run at approximately the same speed on all computers, while the continously cycling loop will run as quickly as a computer can process it -- faster on fast computers, and slower on slow computers.

timer 250, [DoRandom]

[DoRandom]
    num = int(rnd(0)*2000)
    print  num
    wait

USE A CONTINUOUS SCAN LOOP WITH CAUTION!

Carl Gundel says, [the program might slow down] "if you stick in more than one SCAN per loop, which is sometimes not a good idea for various reasons. SCAN is probably best used at the end of a loop. If you use it at the beginning, this can sometimes

prevent the contents of the loop from executing. On the other hand, if this is the design intention, then you can do it that way. The concern is that while your Liberty BASIC program runs continuously this will slow down everything else on your system that isn't your program."


Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index