inputif statement

The inputif statement captures input from the type-ahead buffer and assigns the input to a variable.

Syntax

 inputif var {= expr} {,length-expr} {,fillchars} {_} 
 {:} {from line-expr} 
         then/else statement
 end

Parameter(s)

: A colon suppresses the automatic line feed and carriage return when the value is input.
_ An underscore specifies that a carriage return must be entered by the user, even if the input length equals length-expr.

If the user tries to exceed the maximum length, a bell rings.

var = expr Assigns input data to variable var. If expr is specified, var is initially set to the value of expr, and characters in the type-ahead buffer are appended to that value.
length-expr An expression, up to 140 characters, to be interpreted as the maximum number of characters to assign to var.
fillchars An expression evaluating to a string of one, two, or three characters.

The first character is taken as a background mask for the input field, and the second is used as an overstrike filler for portions of the field left unwritten. The cursor returns to the end of the input data unless there is a third fill character. If there is a third fill character, the cursor remains at the end of the formatted field.

FROM line-expr Take the input from the type-ahead of the attached line line-expr.

If the line is not attached, a run-time error is printed and the BASIC Debugger is entered.

THEN statement Executes a statement if the type-ahead buffer is not empty.
ELSE statement Executes a statement if the type-ahead buffer is empty.

Description

The inputif statement checks the type-ahead buffer and, if it is non-null, initially assigns the variable var with the contents of the type-ahead buffer.

After the contents are assigned, it executes the THEN part of the statement. If a maximum length (length-expr) has been specified with an underscore (_), pressing Enter is necessary. If there is no data in the type-ahead buffer, the variable is not assigned and the ELSE part of the statement is executed.

If the type-ahead buffer is empty, the variable is not assigned and the user is not prompted. However, if the type-ahead buffer is not empty, but a carriage return is required to complete the input, the user is prompted with the type-ahead buffer input.

The inputif statement includes many features reflecting those of the standard input statement. See input statement for more information.

The type-ahead feature can be controlled with the ta statement. The ta statement has options to turn on or off type-ahead and to clear the type-ahead buffer. Furthermore, the intrinsic function ta returns the number of characters in the type-ahead buffer.

Example(s)

Example 1

To assign the variable TYPEA with the current contents of the type-ahead buffer and print "TYPE AHEAD EMPTY" if no data is received, the code would read:

 INPUTIF TYPEA ELSE
    PRINT "TYPE AHEAD EMPTY"
 END

Example 2

In this example, a requested calculation might take several minutes to complete. The user is allowed to use the Esc key to exit from the program, and the inputif statement is repeated at every iteration to see if Escape was entered in the type-ahead buffer.

 PRINT "ENTER <ESC> TO EXIT."
 LOOP
    .
    .
    .
    LOOP
       INPUTIF STOPVAR,1 :  THEN         
          IF STOPVAR = CHAR(27) THEN       
             GOSUB EXIT                   
          END
          TA.EMPTY = 0                  
       END ELSE
          TA.EMPTY = 1                
       END
    UNTIL TA.EMPTY DO REPEAT              
    .
    .
    .
  UNTIL ...DO REPEAT