Difference between revisions of "Line numbers"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
(Created page with ''''Line numbers''' or line labels are used in Qbasic as references to code lines. Numbers are required with GOTO and numbers or label names are required with GOSUB. Altho…')
 
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Line numbers''' or line labels are used in Qbasic as references to code lines. Numbers are required with [[GOTO]] and numbers or label names are required with [[GOSUB]]. Although not required in Qbasic, other older programming languages such as GW Basic often required them. Line labels require a colon after them to differentiate them from keywords and variable names.
'''Line numbers''' or '''line labels''' are used to denote [[GOTO]], [[RUN]] or [[GOSUB]] procedure lines or all code lines as in GW Basic.




The following program can remove line numbers from code where they are not required:  
== Number Syntax ==
{{CodeStart}} '' ''
:: '''10''' [[GOTO]] {line number| line label}
{{Cl|CLS}}
{{Cl|REDIM}} linenumber(5000)
{{Cl|PRINT}} "This Program removes unnecessary line numbers.": {{Cl|PRINT}}
{{Cl|INPUT}} "Name of file: ", filename$: {{Cl|PRINT}}
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
{{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
{{Cl|REM}} Get rid of quotes and {{Cl|REM}} statements.
DO
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) <> 0 {{Cl|THEN}}
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) = 0 {{Cl|THEN}} {{Cl|EXIT DO}}: {{Cl|REM}} Mismatched quotes. Illegal code.
a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) - 1) + {{Cl|MID$}}(a$, {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) + 1)
{{Cl|ELSE}}
{{Cl|EXIT DO}}
{{Cl|END IF}}
{{Cl|LOOP}}


{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} "))
== Label Syntax ==
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "' ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, "' "))
::'''PictureData:'''
:: [[DATA]] 0, 0, 12, 12, 14, 12, 12, 0, 0


DO
flag = 0
{{Cl|REM}} Break down compound statements and get line numbers.
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") + 6: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|ERL}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") + 4: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} {{Cl|EXIT DO}} {{Cl|ELSE}} a$ = {{Cl|MID$}}(a$, n)
{{Cl|LOOP}}
{{Cl|LOOP}}
{{Cl|PRINT}} "Number of line-numbered statements kept:"; count
{{Cl|CLOSE}} #1


{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
* Line numbers are used to denote a specific line of code. No colon required.
{{Cl|OPEN}} "filename.tmp" {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #2
* Line labels are used to denote a specific line of code with a [[colon]] when separating it from a code line.
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
* [[GOSUB]] or [[DATA]] block line labels can be numerical or text line names. [[RESTORE]] can use the label to reuse data.  
{{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
* [[GOSUB]] blocks require a [[RETURN]] to return to the original call or to a line label or number to return to.
flag = 0
* [[ON ERROR]] [[GOTO]] line label or number calls use [[RESUME]] [[NEXT]] or a line label or number to resume to.
{{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, 1, 1)) <> 0 {{Cl|THEN}}
* [[GOTO]] or [[RUN]] can refer to a specific numerical line number or text line label.
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} count
* Line numbers are no longer required in QB or QB64 except for [[GOSUB]], [[RUN]] or [[GOTO]] situations.
{{Cl|IF...THEN|IF}} {{Cl|VAL}}(a$) = linenumber(i) {{Cl|THEN}} flag = 1: {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
* Line numbers and labels are not allowed after SUB/FUNCTION blocks. They are allowed inside SUB/FUNCTIONS though.
{{Cl|NEXT}}
{{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, {{Cl|INSTR}}(a$, " ") + 1)
{{Cl|END IF}}
{{Cl|PRINT (file statement)|PRINT}} #2, a$
{{Cl|LOOP}}
{{Cl|CLOSE}} #1, #2
{{Cl|SHELL}} "cmd/c start notepad filename.tmp"
{{Cl|PRINT}}
{{Cl|PRINT}} "Name the line edited file as the original file and backup original file? [Y/N]"
DO
b$ = {{Cl|UCASE$}}({{Cl|INKEY$}})
{{Cl|IF...THEN|IF}} b$ = "N" {{Cl|OR (boolean)|OR}} b$ = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|IF...THEN|IF}} b$ = "Y" {{Cl|THEN}}
{{Cl|ON ERROR}} {{Cl|GOTO}} resnxt
{{Cl|KILL}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
{{Cl|ON ERROR}} {{Cl|GOTO}} 0
{{Cl|NAME}} filename$ {{Cl|AS}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
{{Cl|NAME}} "filename.tmp" {{Cl|AS}} filename$
{{Cl|PRINT}} : {{Cl|PRINT}} "File conversion completed."
{{Cl|EXIT DO}}
{{Cl|END IF}}
{{Cl|LOOP}}
{{Cl|SYSTEM}}
 
tracknumber:
DO
count = count + 1
linenumber(count) = {{Cl|VAL}}({{Cl|MID$}}(a$, n))
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}}") <> 0 {{Cl|OR (boolean)|OR}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}}") <> 0 {{Cl|THEN}}
{{Cl|FOR...NEXT|FOR}} i = n {{Cl|TO}} {{Cl|LEN}}(a$)
{{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, i, 1)) = 0 {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "0" {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "," {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> " " {{Cl|THEN}} {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
{{Cl|IF...THEN|IF}} {{Cl|MID$}}(a$, i, 2) = ", " {{Cl|THEN}} n = i + 1: {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
{{Cl|NEXT}}
{{Cl|IF...THEN|IF}} n <> i + 1 {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|ELSE}}
{{Cl|EXIT DO}}
{{Cl|END IF}}
{{Cl|LOOP}}
{{Cl|RETURN}}
 
resnxt:
{{Cl|RESUME NEXT}} '' ''
{{CodeEnd}}
{{small|Code courtesy of Pete from the N54 Qbasic Forum}}




''See also:''
''See also:''
* [[GOTO]]
* [[GOTO]], [[RUN]]  
* [[GOSUB]]
* [[GOSUB]], [[RETURN]]
* [[DATA]], [[RESTORE]]
* [[ON ERROR]], [[RESUME]], [[NEXT]]
* [[Line_number|Line number removal procedures]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 14:47, 16 September 2020

Line numbers or line labels are used to denote GOTO, RUN or GOSUB procedure lines or all code lines as in GW Basic.


Number Syntax

10 GOTO {line number| line label}

Label Syntax

PictureData:
DATA 0, 0, 12, 12, 14, 12, 12, 0, 0


  • Line numbers are used to denote a specific line of code. No colon required.
  • Line labels are used to denote a specific line of code with a colon when separating it from a code line.
  • GOSUB or DATA block line labels can be numerical or text line names. RESTORE can use the label to reuse data.
  • GOSUB blocks require a RETURN to return to the original call or to a line label or number to return to.
  • ON ERROR GOTO line label or number calls use RESUME NEXT or a line label or number to resume to.
  • GOTO or RUN can refer to a specific numerical line number or text line label.
  • Line numbers are no longer required in QB or QB64 except for GOSUB, RUN or GOTO situations.
  • Line numbers and labels are not allowed after SUB/FUNCTION blocks. They are allowed inside SUB/FUNCTIONS though.


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page