Use of go command: Difference between revisions
Created page with "{{DISPLAYTITLE:Enhanced IF Statement}} Programming Features - <strong>Use of GO() Command</strong> Color 64 adds GO() as a conditional expression function. It selects and returns one of two possible values based on whether a condition is true or false. GO() is similar to placing a small IF/ELSE decision inside an expression. It may be used in assignments, calculations, string construction, output statements, function arguments, and other locati..." |
No edit summary |
||
| Line 1: | Line 1: | ||
{{DISPLAYTITLE: | {{DISPLAYTITLE:Use of GO() Command}} | ||
[[programming features|Programming Features]] - <strong>Use of GO() Command</strong> | [[programming features|Programming Features]] - <strong>Use of GO() Command</strong> | ||
Latest revision as of 16:18, 29 July 2026
Programming Features - Use of GO() Command
Color 64 adds GO() as a conditional expression function. It selects and returns one of two possible values based on whether a condition is true or false.
GO() is similar to placing a small IF/ELSE decision inside an expression. It may be used in assignments, calculations, string construction, output statements, function arguments, and other locations where BASIC expects a value.
Unlike an ordinary IF...THEN statement, GO() returns a value.
Syntax GO(condition,true-result,false-result)
The three required parts are:
- condition is the expression tested by GO().
- true-result is returned when the condition is true.
- false-result is returned when the condition is false.
Both commas and the closing parenthesis are required.
Numeric Conditions
For a numeric condition: |0 = False |Nonzero = True
Example:
100 A=GO(X>10,1,0)
When X is greater than 10, A is set to 1. Otherwise, A is set to 0.
Any nonzero numeric value is considered true, including negative values.
100 A=GO(X,50,25)
If X is nonzero, A becomes 50. If X is zero, A becomes 25.
String Conditions
A string may also be used as the condition: |Null string = False |Nonempty string = True
Example:
100 A$=GO(FI$,"FILE: "+FI$,"NO FILE")
If FI$ contains one or more characters, the first result is returned. If FI$ is empty, "NO FILE" is returned. This provides a compact way to test whether a string contains data without separately checking its length.
Numeric Results
GO() may return numeric values or numeric expressions.
100 A=GO(X=5,100,200)
If X equals 5, A becomes 100. Otherwise, A becomes 200.
The result expressions do not have to be constants:
100 A=GO(X>Y,X*2,Y*2)
If X is greater than Y, the result is X*2. Otherwise, the result is Y*2.
String Results
GO() may return strings or string expressions.
100 A$=GO(ER=0,"OK","ERROR")
String expressions may include variables, concatenation, and other string functions:
100 A$=GO(FI$<>"","OPENING "+FI$,"NO FILENAME")
Using GO() in Output
Because GO() returns a value, it may be used directly inside an output statement.
100 PRINT GO(X,"YES","NO")
It may also be combined with other text:
100 PRINT "WORD WRAP ";GO(WW,"ON","OFF")
An actual Color 64-style use is:
100 %GO(FM,"WORDWRAP ON","WORDWRAP OFF")
Lazy Evaluation
An important feature of GO() is that it evaluates only the result that is selected.
When the condition is true:
- The true result is evaluated.
- The false result is skipped.
When the condition is false:
- The true result is skipped.
- The false result is evaluated.
This differs from functions that evaluate every argument before selecting a result.
Avoiding Invalid Calculations
Lazy evaluation can prevent an error in an unused expression.
100 A=GO(D<>0,N/D,0)
If D is zero, N/D is skipped and the function returns zero. A division-by-zero error does not occur.
Without lazy evaluation, both result expressions would be calculated and the division would fail even though its result was not needed.
Avoiding Invalid String Operations
100 A$=GO(LEN(I$)>0,LEFT$(I$,1),"")
When I$ is empty, the LEFT$ expression is skipped and a null string is returned.
Nested GO() Functions
A GO() function may be placed inside another GO() function.
100 A$=GO(X=1,"ONE",GO(X=2,"TWO","OTHER"))
This produces:
- X = 1 returns "ONE"
- X = 2 returns "TWO"
- Other returns "OTHER"
Nested use can replace several small IF tests, but deeply nested expressions may become difficult to read and maintain.
For more complicated decisions, ordinary IF...THEN, ON...GOTO, or a subroutine may be clearer.
Comparisons as Conditions
Any BASIC comparison may be used as the condition:
- GO(A=B,...,...)
- GO(A<>B,...,...)
- GO(A>B,...,...)
- GO(A<B,...,...)
- GO(A>=B,...,...)
- GO(A$="Y",...,...)
Examples:
100 A$=GO(LV>=9,"SYSOP","CALLER") 100 A=GO(CR>0,CR-1,0)
Function Calls Within GO()
The condition and result sections may contain other BASIC or Color 64 functions:
100 A$=GO(LEN(FI$)>15,LEFT$(FI$,15),FI$)
100 A=GO(@2("A",I$),1,0)
Commas inside nested function parentheses are handled correctly:
100 A$=GO(X,LEFT$(I$,10),RIGHT$(I$,10))
Commas or parentheses inside quoted strings are treated as ordinary characters:
100 A$=GO(X,"YES, CONTINUE","NO, STOP")
Result Types
The selected result may be numeric or string.
100 A=GO(X,1,0)
returns a numeric value.
100 A$=GO(X,"YES","NO")
returns a string value.
The two result expressions can technically be different types:
100 X=GO(A,10,"ERROR")
However, this is not recommended. The type returned changes according to the condition, and the receiving expression may produce a TYPE MISMATCH.
For predictable operation, both result expressions should normally return the same type:
GO(condition,number,number)
or:
GO(condition,string,string)
What GO() Can Do
GO() can:
- Select between two numeric values.
- Select between two strings.
- Use numeric comparisons as conditions.
- Use an empty or nonempty string as a condition.
- Return calculations and complete expressions.
- Be used inside larger expressions.
- Be used in PRINT and Color 64 output commands.
- Be passed as an argument to another function.
- Contain nested BASIC functions.
- Contain another GO() function.
- Avoid evaluating an unsafe or unnecessary expression.
- Replace simple IF/ELSE value-selection code.
What GO() Cannot Do
- GO() does not replace BASIC program-control statements.
The true and false sections must be expressions that return values. They cannot contain statement commands such as:
- GOTO
- GOSUB
- RETURN
- INPUT
- POKE
- OPEN
- CLOSE
- FOR
- NEXT
For example, this is not valid:
100 GO(X,GOTO 500,GOTO 600)
Use an ordinary IF statement instead:
100 IF X THEN GOTO 500 110 GOTO 600
GO() also cannot execute a series of colon-separated statements:
100 GO(X,A=1:B=2,A=3:B=4)
Assignments and statement sequences must remain outside the function.
- GO() Does Not Mean GOTO
- GO() is a value-returning function.
It does not branch to a BASIC line number.
A=GO(X,100,200)
sets A to either 100 or 200. It does not transfer execution to line 100 or line 200.
To branch between line numbers, use IF...THEN or ON...GOTO
Common Uses On/Off Text
100 PRINT "MODE: ";GO(MD,"ON","OFF")
Default Value
100 A$=GO(FI$,FI$,"UNTITLED")
Minimum Value
100 A=GO(X<0,0,X)
Maximum Limit
100 A=GO(X>255,255,X)
Safe Division
100 A=GO(D,N/D,0)
Optional Carriage Return
100 I$=@8+GO(!60,CR$,"")
Display Width
100 PRINT GO(MC>40,"80 COLUMN","40 COLUMN")
Nested Selection
100 A$=GO(LV>=9,"SYSOP",GO(LV>=5,"MODERATOR","CALLER"))
Programming Recommendations
Use GO() when choosing between two values as part of an expression.
It is especially useful for:
- Short text substitutions.
- On/off descriptions.
- Default values.
- Safe calculations.
- Compact comparison results.
- Avoiding several nearly identical IF statements.
Use a normal IF...THEN structure when:
- Program execution must branch to another line.
- Variables must be assigned in multiple statements.
- Several commands must be performed.
- The decision requires lengthy or complicated processing.
- Nested GO() expressions would make the program difficult to read.
Next Section: Shorthand Numeric Updates