ON...GOSUB: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "ON...GOSUB is a control-flow statement that branches to a line or label in a list depending on a numerical expression. {{PageSyntax}} : '''ON''' {{Parameter|numericalExpression}} GOSUB {{Parameter|labelOrNumber}}[,{{Parameter|labelOrNumber}}][,...] {{PageDescription}} * {{Parameter|numericalExpression}} represents the ''line'' or ''label'' that the program should branch to: 1 branches to the first line or label in the list, 2 branches to the second, etc. * Th...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:
* '''Note:''' [[SELECT CASE]] provides a much more convenient way of doing this task.
* '''Note:''' [[SELECT CASE]] provides a much more convenient way of doing this task.


 
=== QBasic/QuickBASIC ===
==QBasic/QuickBASIC==
* In QuickBASIC 4.5 the list could contain a maximum of 60 line numbers or labels, while '''QB64''' has no limit.
* In QuickBASIC 4.5 the list could contain a maximum of 60 line numbers or labels, while '''QB64''' has no limit.


Line 19: Line 18:
{{PageExamples}}
{{PageExamples}}
''Example:''
''Example:''
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|CLS}}
{{Cl|CLS}}
a = 2
a = 2
''ON'' a '''GOSUB''' hello, hereweare, 143
{{Cl|ON...GOSUB|ON}} a {{Cl|ON...GOSUB|GOSUB}} hello, hereweare, 143
{{Cl|PRINT}} "Also notice the RETURN statement that can be used with GOSUB!"
{{Cl|PRINT}} "Also notice the RETURN statement that can be used with GOSUB!"
{{Cl|END}}
{{Cl|END}}
Line 36: Line 35:
143
143
{{Cl|PRINT}} "Line 143, with a = 3 you get to see this!"
{{Cl|PRINT}} "Line 143, with a = 3 you get to see this!"
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 48: Line 47:
{{PageSeeAlso}}
{{PageSeeAlso}}
* [[ON...GOTO]]
* [[ON...GOTO]]
* [[GOSUB]], [[GOTO]]  
* [[GOSUB]], [[GOTO]]
* [[SELECT CASE]], [[RETURN]]
* [[SELECT CASE]], [[RETURN]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:05, 21 February 2023

ON...GOSUB is a control-flow statement that branches to a line or label in a list depending on a numerical expression.


Syntax

ON numericalExpression GOSUB labelOrNumber[,labelOrNumber][,...]


Description

  • numericalExpression represents the line or label that the program should branch to: 1 branches to the first line or label in the list, 2 branches to the second, etc.
  • The procedure must be used after the number value is determined or in a loop to monitor current user events.
  • RETURN returns to the next code statement after the ON...GOSUB statement. END or SYSTEM can be used to end program.
  • Note: SELECT CASE provides a much more convenient way of doing this task.

QBasic/QuickBASIC

  • In QuickBASIC 4.5 the list could contain a maximum of 60 line numbers or labels, while QB64 has no limit.


Examples

Example:

CLS
a = 2
ON a GOSUB hello, hereweare, 143
PRINT "Also notice the RETURN statement that can be used with GOSUB!"
END

hello:
PRINT "Hello, with a = 1 you get to see this!"
END

hereweare:
PRINT "with a = 2 here we are...return to line after ON."
RETURN

143
PRINT "Line 143, with a = 3 you get to see this!"
END
with a = 2 here we are...return to line after ON.
Also notice the RETURN statement that can be used with GOSUB!
Explanation: Since a equals to 2 it goes to the second item in the list (hereweare) and branches the program to there. Try changing 'a' to 1 or 3.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link