Difference between revisions of "PRINT"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The {{KW|PRINT}} statement prints numeric or string expressions to the screen.
+
The {{KW|PRINT}} statement prints numeric or string expressions to the program screen. Typing shortcut '''?'''  will convert to PRINT.
  
  
 
{{PageSyntax}}
 
{{PageSyntax}}
:<code>{{KW|PRINT}} [ [{{Parameter|expression}}] [{{Parameter|separator}}] ... ]</code>
+
::: '''PRINT''' [{{Parameter|expression}}] [{;|,] [''expression''...]
  
  
{{PageDescription}}
+
{{Parameters}}
* {{Parameter|expression}} is a numeric or string expression to be printed.
+
* {{Parameter|expression}} is a numeric or string expression or list of expressions to be written to the screen. End quotes will not be displayed in strings.
** {{KW|SPACE$}}({{Parameter|n%}}) or {{KW|SPC}}({{Parameter|n%}}) - specifies that {{Parameter|n%}} space characters will be printed.
+
* The print statement can be followed by a [[semicolon]] to stop the print cursor or a [[comma]] to tab space the next print.
** {{KW|TAB}}({{Parameter|column%}}) - specifies that the print cursor is to move to column number {{Parameter|column%}}. If the print cursor is beyond that column, it is moved to that column on the next line.
 
** PRINT can also use the + '''concatenation''' operator to combine string values only.
 
* {{Parameter|separator}} is used to separate multiple expressions and specifies how the print cursor is to be moved before printing the next expression. It can be one of the following:
 
** semi-colon (;) - specifies no change in the print cursor.
 
** comma (,) - specifies that the print cursor is to move to the next 14-column tab-stop. If the print cursor is at column 56 or greater, it is moved to the next line at column 1.
 
* If {{Parameter|separator}} is not used at the end of the expression list, the print cursor moves to the next line at column 1.
 
  
  
* When printing numeric expressions, the following rules are used:
+
''Usage:''
 +
* [[STRING]] values will eliminate leading and trailing quotation marks when printed to the screen. Use [[CHR$]](34) to add quotes to the [[SCREEN]].
 +
* PRINT with no parameters moves the print cursor to the next print row at column 1.
 +
* {{Parameter|expression}} is a numeric or string expression to be printed.
 +
** [[SPACE$]]({{Parameter|n%}}) or [[SPC]]({{Parameter|n%}}) - specifies that {{Parameter|n%}} space characters will be printed.
 +
** [[TAB]]({{Parameter|column%}}) - specifies that the print cursor is to move to column number {{Parameter|column%}}. If the print cursor is already beyond that column, it is moved to the designated column on the next row.
 +
* A ''separator'' is used to separate multiple expressions and specifies how the print cursor is to be moved:
 +
** [[Semicolon]](;) - specifies that the print cursor stops at the end of the printed ''expression'' and may append later ''expressions'' or prints. PRINT ; or PRINT ""; will stop cursor movement and append later prints. Ending [[semicolon]]s can also stop screen roll.
 +
** [[Comma]](,) - specifies that the print cursor is to move to the next 14-column tab-stop. If the print cursor is at column 56 or greater, it is moved to the next row at column 1. When used after an ''expression'' it may Tab-stop append later prints.
 +
** [[+|Plus]](+) uses [[concatenation]] to add [[STRING]] expressions ONLY with no spacing. '''Cannot combine  numerical ''expression''s!'''
 +
** If a {{Parameter|separator}} is not used at the end of the expression list, the print cursor moves to the next row at column 1.
 +
* When '''printing numerical''' ''expressions'' values, the following rules are used:
 
** If the value is positive, the number is prefixed with a space character, otherwise, the number is prefixed with a negative sign (-).
 
** If the value is positive, the number is prefixed with a space character, otherwise, the number is prefixed with a negative sign (-).
 
** If the value is an integer (whole number), no decimal point or fractional part will be printed.
 
** If the value is an integer (whole number), no decimal point or fractional part will be printed.
** If the value is not an integer (whole number) and has zero for a coefficient, no leading zero is printed. For example, <code>-0.123</code> is printed as <code>"-.123 "</code>
+
** If the value is not an [[INTEGER|integer]](whole number) and has zero for a coefficient, no leading zero is printed. EX: -0.123 prints "-.123 "
** If the expression is a numeric literal and is in scientific notation, the number is also printed in scientific notation.
+
** If the expression is in [[scientific notation]], the number is also printed in scientific notation.
** The number is suffixed with a space character unless {{KW|STR$}}(number) is used.
+
** The number is [[PRINT|printed]] with a space after it unless [[STR$]](number) is used to convert it to string text.
** Numerical values MUST be added to a PRINT statement string using commas or semicolons on each side of the value or by using {{KW|STR$}} to convert the value to a string to use string '''concatenation (+ string addition).'''
+
** Numerical values MUST be added to a PRINT statement string using [[comma]]s or [[semicolon]]s on each side of the value or by using [[STR$]] to convert the value to a string to use [[concatenation]] (+ string addition).
 
+
* [[VIEW PRINT]] can set up a viewport area for PRINTs. Text printed on the bottom view port row will scroll the text upward.  
 
+
* Text to be printed can be a [[STRING]] variable or a literal value inside of quotation marks.  
* {{KW|VIEW PRINT}} can set up a viewport area for PRINTs.
+
* Use [[semicolon]] ends on bottom 2 rows of the SCREEN mode used or the PRINT will roll the screen up.  
* Whenever {{KW|PRINT}} moves the print cursor to a new line, the text viewport is scrolled if necessary.  
+
* '''Quotes cannot be inside of a literal STRING! Use [[CHR$]](34) [[concatenation]] to insert [[quotation mark]]s into a literal string.'''  
* Text to be printed can be a {{KW|STRING}} variable or a literal value inside of quotation marks.
 
* A semicolon at the PRINT string end stops the print cursor immediately for the next PRINT to be appended.  
 
* Use semicolon ends on bottom 2 rows of the SCREEN mode used or the PRINT will roll the screen up.
 
* Commas {{KW|TAB}} the print cursor for next data. You can use more than one consecutively.
 
* '''Quotes cannot be inside of the STRING! Use {{KW|CHR$}}(34) to place quotation marks inside of a literal string.'''  
 
 
* To better format number and text data placement use {{KW|PRINT USING}}.
 
* To better format number and text data placement use {{KW|PRINT USING}}.
* Instead of typing PRINT you can just type a question mark (?). It will change to PRINT when enter is pressed in the IDE.
+
* Instead of typing PRINT you can just type a [[question mark]] (?). It will change to PRINT when enter is pressed in the IDE.
 +
* Use the [[_PRINTMODE]] statement before a print to deal with the text background in '''QB64''':
 +
:*'''1''' _KEEPBACKGROUND: Text background transparent. Only the text is displayed over anything behind it.
 +
:*'''2''' _ONLYBACKGROUND: Text background is only displayed. Text is transparent to anything behind it.
 +
:*'''3''' _FILLBACKGROUND: Text and background block anything behind them. Default setting.
 +
:* Use the [[_PRINTMODE (function)]] to find the current _PRINTMODE setting number.
 +
* [[WRITE]] can be used to print a list of comma separated data values to the screen with [[comma]]s between each value.
 +
* Use [[_DEST]] [[_CONSOLE]] before PRINT statements to be used in a [[$CONSOLE|console]] window.
 +
* Use [[_CONTROLCHR]] '''OFF''' to PRINT the unprintable lower [[ASCII]] control characters in QB64.  
  
  
{{PageExamples}}
+
''Example 1:'' Using semicolons, comma tabs or concatenation to insert [[ASCII]] characters and numbers in a PRINT:
:Using semicolons, comma tabs or concatenation to insert numbers in a PRINT string:
 
 
{{CodeStart}}
 
{{CodeStart}}
 
{{Cl|PRINT}} CHR$(34); "Hello world!"; CHR$(34) ' adding quotation marks
 
{{Cl|PRINT}} CHR$(34); "Hello world!"; CHR$(34) ' adding quotation marks
 
{{Cl|PRINT}} 123 'demonstrates the positive leading space
 
{{Cl|PRINT}} 123 'demonstrates the positive leading space
a$ = "Hello country!"
+
a$ = "Hello country!": a = 321: b = -321
a = 321
 
 
{{Cl|PRINT}} a$, a ' demonstrates comma in statement
 
{{Cl|PRINT}} a$, a ' demonstrates comma in statement
 
{{Cl|PRINT}} a$; a ' demonstrates semicolon in statement
 
{{Cl|PRINT}} a$; a ' demonstrates semicolon in statement
{{Cl|PRINT}} a$ + {{Cl|STR$}}(a) ' concatenation of string numerical values only
+
{{Cl|PRINT}} a$ + {{Cl|STR$}}(b) ' concatenation of string numerical values only
? "Hello city!" ' a ? changes to PRINT after moving cursor from the code line
+
? "Hello city!" ' a ? changes to PRINT after moving cursor from the code line in IDE
 
{{CodeEnd}}
 
{{CodeEnd}}
{{OutputStart}}
+
{{OutputStart}}"Hello world!"
"Hello world!"
 
 
  123
 
  123
 
Hello country!      321
 
Hello country!      321
 
Hello country! 321
 
Hello country! 321
Hello country! 321
+
Hello country!-321
 
Hello city!
 
Hello city!
 
{{OutputEnd}}
 
{{OutputEnd}}
:First PRINT prints the text between the quotation marks, then it prints the value 123, notice that there are no quotation marks when printing the value, quotation marks means that it will be treated like a string of text.
+
:First PRINT prints the text between two quotation marks, then it prints the value 123, notice that there are no quotation marks when printing the value, quotation marks mean that it will be treated like a literal string of text. a$ is set to "Hello country" and 'a' is set to the value 321, the dollar sign is used when a variable holds the text string. The contents of a$ is then printed and the "," means that the value of 'a' is printed separated by a tab and ";" means that there is no separation from the other text except for the leading positive value space.
:a$ is set to "Hello country" and 'a' is set to the value 321, the dollar sign is used when a variable handles text. The contents of a$ is then printed and the "," means that the value of 'a' is printed seperated by a tab and ";" means that there are no separation from the other text.
+
 
 +
 
 +
''Example 2:'' Changing colors in a line of text using semicolons with colon separators between PRINTs on the same code line.
 +
{{CodeStart}}
 +
{{Cl|COLOR}} 12: {{Cl|PRINT}} "Start red "; : {{Cl|COLOR}} 10: {{Cl|PRINT}} "and end green."
 +
{{Cl|COLOR}} 11: {{Cl|PRINT}} "Start aqua ";
 +
{{Cl|COLOR}} 14: {{Cl|PRINT}} "and end blue."
 +
{{CodeEnd}}
 +
{{OutputStart}}{{text|Start red|#FF1515}} {{text|and end green.|#15FF15}}
 +
{{text|Start aqua|#15FFFF}} {{text|and end blue.|#0000FF}} {{OutputEnd}}
  
  
 
{{PageSeeAlso}}
 
{{PageSeeAlso}}
* {{KW|SPC}}, {{KW|TAB}}
+
* [[_PRINTMODE]], [[_PRINTSTRING]], [[PRINT USING]]
* {{KW|CSRLIN}}, {{KW|POS}}, {{KW|COLOR}}, {{KW|LOCATE}}, {{KW|CHR$}}, {{KW|VIEW PRINT}}, {{KW|SCREEN (statement)|SCREEN}}
+
* [[SPC]], [[TAB]], [[SPACE$]], [[SCREEN]]
* {{KW|SPACE$}}, {{KW|INPUT}}, {{KW|STR$}}, {{KW|SCREEN (function)}}
+
* [[CSRLIN]], [[POS]], [[SCREEN (function)]]
 +
* [[COLOR]], [[LOCATE]], [[VIEW PRINT]]
 +
* [[INPUT]], [[STR$]], [[CHR$]]
 +
* [[ASCII]] (character codes), [[_CONTROLCHR]]
 +
* [[Text Using Graphics]] (Demo)
  
  
 
{{PageNavigation}}
 
{{PageNavigation}}

Latest revision as of 21:41, 28 November 2016

The PRINT statement prints numeric or string expressions to the program screen. Typing shortcut ? will convert to PRINT.


Syntax

PRINT [expression] [{;|,] [expression...]


Parameters

  • expression is a numeric or string expression or list of expressions to be written to the screen. End quotes will not be displayed in strings.
  • The print statement can be followed by a semicolon to stop the print cursor or a comma to tab space the next print.


Usage:

  • STRING values will eliminate leading and trailing quotation marks when printed to the screen. Use CHR$(34) to add quotes to the SCREEN.
  • PRINT with no parameters moves the print cursor to the next print row at column 1.
  • expression is a numeric or string expression to be printed.
    • SPACE$(n%) or SPC(n%) - specifies that n% space characters will be printed.
    • TAB(column%) - specifies that the print cursor is to move to column number column%. If the print cursor is already beyond that column, it is moved to the designated column on the next row.
  • A separator is used to separate multiple expressions and specifies how the print cursor is to be moved:
    • Semicolon(;) - specifies that the print cursor stops at the end of the printed expression and may append later expressions or prints. PRINT ; or PRINT ""; will stop cursor movement and append later prints. Ending semicolons can also stop screen roll.
    • Comma(,) - specifies that the print cursor is to move to the next 14-column tab-stop. If the print cursor is at column 56 or greater, it is moved to the next row at column 1. When used after an expression it may Tab-stop append later prints.
    • Plus(+) uses concatenation to add STRING expressions ONLY with no spacing. Cannot combine numerical expressions!
    • If a separator is not used at the end of the expression list, the print cursor moves to the next row at column 1.
  • When printing numerical expressions values, the following rules are used:
    • If the value is positive, the number is prefixed with a space character, otherwise, the number is prefixed with a negative sign (-).
    • If the value is an integer (whole number), no decimal point or fractional part will be printed.
    • If the value is not an integer(whole number) and has zero for a coefficient, no leading zero is printed. EX: -0.123 prints "-.123 "
    • If the expression is in scientific notation, the number is also printed in scientific notation.
    • The number is printed with a space after it unless STR$(number) is used to convert it to string text.
    • Numerical values MUST be added to a PRINT statement string using commas or semicolons on each side of the value or by using STR$ to convert the value to a string to use concatenation (+ string addition).
  • VIEW PRINT can set up a viewport area for PRINTs. Text printed on the bottom view port row will scroll the text upward.
  • Text to be printed can be a STRING variable or a literal value inside of quotation marks.
  • Use semicolon ends on bottom 2 rows of the SCREEN mode used or the PRINT will roll the screen up.
  • Quotes cannot be inside of a literal STRING! Use CHR$(34) concatenation to insert quotation marks into a literal string.
  • To better format number and text data placement use PRINT USING.
  • Instead of typing PRINT you can just type a question mark (?). It will change to PRINT when enter is pressed in the IDE.
  • Use the _PRINTMODE statement before a print to deal with the text background in QB64:
  • 1 _KEEPBACKGROUND: Text background transparent. Only the text is displayed over anything behind it.
  • 2 _ONLYBACKGROUND: Text background is only displayed. Text is transparent to anything behind it.
  • 3 _FILLBACKGROUND: Text and background block anything behind them. Default setting.
  • Use the _PRINTMODE (function) to find the current _PRINTMODE setting number.
  • WRITE can be used to print a list of comma separated data values to the screen with commas between each value.
  • Use _DEST _CONSOLE before PRINT statements to be used in a console window.
  • Use _CONTROLCHR OFF to PRINT the unprintable lower ASCII control characters in QB64.


Example 1: Using semicolons, comma tabs or concatenation to insert ASCII characters and numbers in a PRINT:

PRINT CHR$(34); "Hello world!"; CHR$(34) ' adding quotation marks PRINT 123 'demonstrates the positive leading space a$ = "Hello country!": a = 321: b = -321 PRINT a$, a ' demonstrates comma in statement PRINT a$; a ' demonstrates semicolon in statement PRINT a$ + STR$(b) ' concatenation of string numerical values only ? "Hello city!" ' a ? changes to PRINT after moving cursor from the code line in IDE

"Hello world!" 123 Hello country! 321 Hello country! 321 Hello country!-321 Hello city!

First PRINT prints the text between two quotation marks, then it prints the value 123, notice that there are no quotation marks when printing the value, quotation marks mean that it will be treated like a literal string of text. a$ is set to "Hello country" and 'a' is set to the value 321, the dollar sign is used when a variable holds the text string. The contents of a$ is then printed and the "," means that the value of 'a' is printed separated by a tab and ";" means that there is no separation from the other text except for the leading positive value space.


Example 2: Changing colors in a line of text using semicolons with colon separators between PRINTs on the same code line.

COLOR 12: PRINT "Start red "; : COLOR 10: PRINT "and end green." COLOR 11: PRINT "Start aqua "; COLOR 14: PRINT "and end blue."

Start red and end green. Start aqua and end blue.


See also



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page