Enhanced if statement: Difference between revisions

From Color 64 BBS Wiki
No edit summary
No edit summary
 
Line 2: Line 2:
[[programming features|Programming Features]] - <strong>The Enhanced If/Then Statement</strong>
[[programming features|Programming Features]] - <strong>The Enhanced If/Then Statement</strong>


The Color 64 ML adds a powerful set of decision-making commands that supplement the IF/THEN statement.  
The Color 64 ML introduces enhanced decision-making commands that expand upon the standard BASIC IF/THEN structure.


{| class="wikitable
{| class="wikitable"
|-
|-
|+Enhanced IF/THEN Statements  
|+ Enhanced IF/THEN Statements
! CMD
! CMD
! Details
! Details
|-
|-
| IF/THEN/ELSE  
| IF / THEN / ELSE
| Format: IF <expression> THEN <statements>: £ <statements>  
| Format: IF <expression> THEN <statements> : £ <statements>


The British Pound character “£” represents “ELSE” in Color 64 Basic.  
The British Pound character “£” functions as ELSE in Color 64 BASIC.


Example: IF A=5 THEN PRINT "Hello”: £ PRINT "Goodbye"  
Example:
IF A=5 THEN PRINT "Hello" : £ PRINT "Goodbye"


Result: If A were to equal 5, then "Hello" would be printed; otherwise "Goodbye" would be printed instead.  
Result:
If A equals 5, "Hello" is printed. Otherwise, "Goodbye" is printed.
|-
|-
| AND-ELSE  
| AND-ELSE
|A variation of the ELSE statement is the AND-ELSE statement.  This is represented by the characters '!£'.  It behaves the same as the ELSE statement, except that if the expression was TRUE, then the statements after the '' are executed ALSO (hence both the first AND the second sections are executed, or ELSE just the second section is executed).  
| Represented by the characters "".


Example: IF A=5 THEN PRINT "Hello”:PRINT "Goodbye"
This behaves similarly to ELSE, but with one important difference:
* If the expression is TRUE, both the THEN section and the !£ section execute.
* If the expression is FALSE, only the section executes.


Result: If A were to equal 5 then BOTH "Hello" AND "Goodbye" would be printed, but if the statement were false then only "Goodbye" would be printed. Whatever is after the '' is going to be executed regardless of the outcome of the decision.  
Example:
IF A=5 THEN PRINT "Hello" : !£ PRINT "Goodbye"
 
Result:
If A equals 5, both "Hello" and "Goodbye" are printed.
If A does not equal 5, only "Goodbye" is printed.
 
Whatever follows "" will always execute.
|-
|-
| REDECIDE  
| REDECIDE
| Format: !+ <expression> or  !- <expression>  
| Format: !+ <statement> or  !- <statement>
 
These commands reference the result of the most recent IF/THEN evaluation.


These commands use the result of the most recent IF/THEN statement to decide if the expression is to be executed.  The !+ statement will execute only if the last IF/THEN expression was TRUE. The !- statement will execute only if the last IF/THEN expression was FALSE (hence '+' for TRUE and '-' for FALSE).  
* !+ executes only if the previous IF/THEN evaluated TRUE.
* !- executes only if the previous IF/THEN evaluated FALSE.


Example:  
Example:
  10 IF A=5 THEN PRINT "Hello, ";  
  10 IF A=5 THEN PRINT "Hello, ";
  20 !+PRINT "How Are You?"  
  20 !+ PRINT "How Are You?"
  30 !- PRINT "What is Your Name?"  
  30 !- PRINT "What is Your Name?"


Result: If A was to equal 5, then both the first and second statements would be executed, which would print "Hello, How Are You?".  
Result:
If A equals 5:
: "Hello, How Are You?" is printed.


Conversely, if A was not 5 then only the REDECIDE-FALSE (the !-) statement would be executed, which would print "What is Your Name?"  
If A does not equal 5:
: Only "What is Your Name?" is printed.


You can also use ELSE with the REDECIDE statements. ELSE will execute if the opposite of the REDECIDE statement is true.  
ELSE may also be combined with REDECIDE statements. In this case, the £ portion executes when the opposite condition of the REDECIDE is true.


Example:
Example:
  10 IF A=5 THEN PRINT"Joe":£ PRINT"Jim "  
  10 IF A=5 THEN PRINT "Joe" : £ PRINT "Jim"
  20 !+ PRINT"Jenna ":£ PRINT"Jessi "
  20 !+ PRINT "Jenna" : £ PRINT "Jessi"
  30 !- PRINT"Jerry ":£ PRINT"John "
  30 !- PRINT "Jerry" : £ PRINT "John"
  40 !+ PRINT"Jack":£ PRINT"Frank"  
  40 !+ PRINT "Jack" : £ PRINT "Frank"


Result: If A was to equal 5, then Joe, Jenna, John and Jack’s names would be printed.  Otherwise, Jim, Jessi, Jerry, and Frank’s names would be printed instead. If A was not 5 then "1 2 3 4" would be printed.  
Result:
If A equals 5:
: Joe, Jenna, John, and Jack are printed.


Note that the execution of a REDECIDE statement, or the alteration of a variable that was in the original IF/THEN statement, does NOT change the true or false nature of the most recent IF/THEN.  
If A does not equal 5:
: Jim, Jessi, Jerry, and Frank are printed.
 
Important:
Executing a REDECIDE statement does not change the TRUE or FALSE status of the most recent IF/THEN. Modifying variables used in the original IF/THEN condition also does not retroactively alter its result.
|}
|}
''' Multiple IF/THEN/ELSE Statements on One Line '''
You may place more than one IF/THEN/ELSE statement on a single line (or any combination of the enhanced decision commands described above). However, it is important to understand how the interpreter evaluates FALSE conditions.
If an IF/THEN expression evaluates FALSE, the interpreter searches forward on the same line for the first “£” (ELSE) symbol that follows that expression.
Example:
IF A=1 THEN C=1 : IF B=1 THEN C=2 : £ C=3
Behavior:
If the expression IF A=1 evaluates FALSE, execution jumps to the first “£” symbol encountered after that expression. In this example, that results in:
C=3
If A=1 evaluates TRUE, then execution continues normally and the second IF/THEN expression is evaluated.
Evaluation results:
A=0, B=0  →  C=3 
A=0, B=1  →  C=3 
A=1, B=0  →  C=3 
A=1, B=1  →  C=2 
When chaining multiple conditional statements on a single line, carefully consider how ELSE resolution will occur. Misplacing a “£” symbol can cause logic to execute differently than intended.


Next Section: [[ml command set|ML Command Set]]
Next Section: [[ml command set|ML Command Set]]


[[programming features|Programming Features]]
[[programming features|Programming Features]]

Latest revision as of 23:14, 27 February 2026

Programming Features - The Enhanced If/Then Statement

The Color 64 ML introduces enhanced decision-making commands that expand upon the standard BASIC IF/THEN structure.

Enhanced IF/THEN Statements
CMD Details
IF / THEN / ELSE Format: IF <expression> THEN <statements> : £ <statements>

The British Pound character “£” functions as ELSE in Color 64 BASIC.

Example:

IF A=5 THEN PRINT "Hello" : £ PRINT "Goodbye"

Result: If A equals 5, "Hello" is printed. Otherwise, "Goodbye" is printed.

AND-ELSE Represented by the characters "!£".

This behaves similarly to ELSE, but with one important difference:

  • If the expression is TRUE, both the THEN section and the !£ section execute.
  • If the expression is FALSE, only the !£ section executes.

Example:

IF A=5 THEN PRINT "Hello" : !£ PRINT "Goodbye"

Result: If A equals 5, both "Hello" and "Goodbye" are printed. If A does not equal 5, only "Goodbye" is printed.

Whatever follows "!£" will always execute.

REDECIDE Format: !+ <statement> or  !- <statement>

These commands reference the result of the most recent IF/THEN evaluation.

  • !+ executes only if the previous IF/THEN evaluated TRUE.
  • !- executes only if the previous IF/THEN evaluated FALSE.

Example:

10 IF A=5 THEN PRINT "Hello, ";
20 !+ PRINT "How Are You?"
30 !- PRINT "What is Your Name?"

Result: If A equals 5:

"Hello, How Are You?" is printed.

If A does not equal 5:

Only "What is Your Name?" is printed.

ELSE may also be combined with REDECIDE statements. In this case, the £ portion executes when the opposite condition of the REDECIDE is true.

Example:

10 IF A=5 THEN PRINT "Joe" : £ PRINT "Jim"
20 !+ PRINT "Jenna" : £ PRINT "Jessi"
30 !- PRINT "Jerry" : £ PRINT "John"
40 !+ PRINT "Jack" : £ PRINT "Frank"

Result: If A equals 5:

Joe, Jenna, John, and Jack are printed.

If A does not equal 5:

Jim, Jessi, Jerry, and Frank are printed.

Important: Executing a REDECIDE statement does not change the TRUE or FALSE status of the most recent IF/THEN. Modifying variables used in the original IF/THEN condition also does not retroactively alter its result.

Multiple IF/THEN/ELSE Statements on One Line

You may place more than one IF/THEN/ELSE statement on a single line (or any combination of the enhanced decision commands described above). However, it is important to understand how the interpreter evaluates FALSE conditions.

If an IF/THEN expression evaluates FALSE, the interpreter searches forward on the same line for the first “£” (ELSE) symbol that follows that expression.

Example:

IF A=1 THEN C=1 : IF B=1 THEN C=2 : £ C=3

Behavior:

If the expression IF A=1 evaluates FALSE, execution jumps to the first “£” symbol encountered after that expression. In this example, that results in:

C=3

If A=1 evaluates TRUE, then execution continues normally and the second IF/THEN expression is evaluated.

Evaluation results:

A=0, B=0  →  C=3  
A=0, B=1  →  C=3  
A=1, B=0  →  C=3  
A=1, B=1  →  C=2  

When chaining multiple conditional statements on a single line, carefully consider how ELSE resolution will occur. Misplacing a “£” symbol can cause logic to execute differently than intended.

Next Section: ML Command Set

Programming Features