PRINT statement

Purpose: Prints to stdout, usually the screen or printer. A question mark, ?, can be used instead of the PRINT statement.

See Remarks section below for using a question mark, ? in a GUI program.


Syntax 1:

 PRINT Comma,Separated,Expressions [;]

Syntax 2:


 ? Comma,Separated,Expressions [;]

Parameters:

  • Comma,Separated,Expressions one or more expressions to be printed. An expression can be a literal or variable number or string, a member of an array or a user defined type, or an inlined function.
  • ; [OPTIONAL] A semi-colon at end of a PRINT expression line suppresses linefeed and causes the next PRINT statement to print on the same line as the previous PRINT statement.

Example 1:


 PRINT "Hello ";
 PRINT "World ";
 PRINT "from BCX"
 

Result:


 Hello World from BCX

Example 2:


 DIM a

 FOR a = 1 TO 10
  PRINT a
 NEXT a

 PRINT "hello ";
 PRINT "world";

 FOR a = 1 TO 10
  PRINT a;
 NEXT a

Result:


  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
 hello world 1 2 3 4 5 6 7 8 9 10

Remarks:

When the PRINT statement is used with a zero or a postive integer, the output is prepended with a single space character. This space is the location for the sign part of the number. When a negative number is printed, a minus sign will be in that location.

PRINT works only in console mode programs however, a question mark, the ? operator, can be used in a GUI program.

The ? operator, when used in a GUI app, is converted to a MSGBOX statement.

Everything to the right of ? must evaluate to a single string expression.

?, with nothing else, is allowed and emits a MSGBOX with only an OKAY button.

Example:


 $BCXVERSION "7.5.2"
 
 GUI "eroteme"
 
  SUB FORMLOAD
    GLOBAL Form1 AS HWND
    Form1 = BCX_FORM("BCX_TEMPLATE", 0, 0, 130, 110)
    BCX_SET_FORM_COLOR(Form1,QBCOLOR(4))
    CENTER(Form1)
    SHOW(Form1)
    ? "Today is " + DATE$ + " and the time is " + TIME$
  END SUB
  
  BEGIN EVENTS
    SELECT CASE CBMSG
    CASE WM_CLOSE
      DestroyWindow(Form1)
      EXIT FUNCTION
  
    END SELECT
  END EVENTS

The PRINT # syntax commonly used in other BASIC dialects for printing to a file is supported in BCX as well as the FPRINT statement. Here's an example.


 OPEN "test.txt" FOR OUTPUT AS #1
 PRINT #1, "this is a test"
 FLUSH  #1
 CLOSE #1
 DIM a$
 
 OPEN "test.txt" FOR INPUT AS #1
 LINE INPUT #1, a$
 PRINT a$
 a$ = ""
 SEEK #1, 0
 LINE INPUT #1, a$
 PRINT a$
 SETEOF #1, 10
 CLOSE #1
 
 OPEN "test.txt" FOR APPEND AS #1
 PRINT #1, "Here", " is", " new", " data"
 CLOSE #1
 
 OPEN "test.txt" FOR INPUT AS #1
 WHILE NOT EOF(#1)
   LINE INPUT #1, a$
   PRINT a$
 WEND
 CLOSE #1
 
 OPEN "test.txt" FOR BINARY AS #1
 CLEAR a$
 GET$ #1, a$, 8
 CLOSE #1
 PRINT a$, "GREAT!"

Result:


 this is a test
 this is a test
 this is a test
 Here is new data
 this is GREAT!

The WRITE statement also can be used for displaying comma delimited and quoted strings to the screen.

PRINT to printer

To send data to a printer, BCX opens a connection to the printer using a handle to PRN, the standard printer communications device. The data then is output to the printer using a PRINT statement.

Here is a simple example of an alternative to LPRINT for sending data to the default printer.


 DIM i
 
 OPEN "PRN" FOR OUTPUT AS FP1
 
 FOR i = 1 TO 60
   PRINT #FP1,"This is line number" , i
 NEXT
 
 PRINT #FP1,CHR$(12) 'CHR$(12) is Form Feed Character

 CLOSE FP1

Printing at column 80 row 25

For the answer to that eternal question, "How is it possible to print to the screen at column 80 row 25 without causing the screen to scroll?" see the example below.


 GLOBAL i, j, A$

 A$ = " : The FastPrint routine is very -QUICK- at displaying text on the screen"

 CLS

 FOR i = 1 TO 24
  j = j + 1 : IF j = 6 THEN j = 1
  FastPrint(4, i, j, 0 , JOIN$(2,STR(i),A$))
 NEXT

 FastPrint(80, 25, 3, 0 , "X")

 KEYPRESS : CLS : END

 SUB FastPrint(Row, Col, Fg, Bg, Text$)
  LOCAL hOut  AS HWND
  LOCAL Coord AS COORD
  LOCAL junk
  Coord.X = Row-1
  Coord.Y = Col-1
  hOut = GetStdHandle(STD_OUTPUT_HANDLE)
  WriteConsoleOutputCharacter(hOut, Text$,    LEN(Text$), Coord, &junk)
  FillConsoleOutputAttribute (hOut, Fg+Bg*16, LEN(Text$), Coord, &junk)
 END SUB

BCX Console Sample Programs using PRINT function.

S22.bas PRINT "The Value Of A! = ", A!

S24.bas PRINT "PI =" & STR$(4.0 *ATN(1.0))

S26.bas PRINT a$

S33.bas PRINT "A = ", a, " and B=", b

S37.bas PRINT "The Factorial Of 34 Is ";Factorial!(34)

S50.bas PRINT "C Macros in BCX. 100 cubed =",cube(100)

S54.bas PRINT "The length of Buffer$ =" , A , " bytes."

S56.bas PRINT C!, " *", A!/C!, " =", A!

S63.bas PRINT Reverse$("Hello There")

S70.bas PRINT

S72.bas PRINT ExeName$()

S74.bas PRINT COMMAND$, " ", FileDateTime$(COMMAND$)

S78.bas PRINT t$, ", ", d$, "(", s$, ")"

S80.bas PRINT h.a

S89.bas PRINT B$[C]

S91.bas PRINT "This program was launched from ",ARGV$[0]

S95.bas PRINT RND

S99.bas PRINT MyType[2,3,4].a

S101.bas PRINT "Windows", OsVersion(), " detected"

S119.bas PRINT CHR$(Sid);

S124.bas PRINT $pB ' Displays "ello!"

S133.bas PRINT "PI = "; ATN(1.0) *4.0