improve docs on if syntax. fixes #81

This commit is contained in:
Irmen de Jong 2022-10-19 23:53:15 +02:00
parent 53b0b562e6
commit 733c17ad3a
2 changed files with 27 additions and 13 deletions

View File

@ -514,17 +514,27 @@ if statements
Conditional execution means that the flow of execution changes based on certiain conditions, Conditional execution means that the flow of execution changes based on certiain conditions,
rather than having fixed gotos or subroutine calls:: rather than having fixed gotos or subroutine calls::
if aa>4 goto overflow
if xx==3 yy = 4
if xx==3 yy = 4 else aa = 2
if xx==5 { if xx==5 {
yy = 99 yy = 99
zz = 42
} else { } else {
aa = 3 aa = 3
bb = 9
} }
if xx==5
yy = 42
else if xx==6
yy = 43
else
yy = 44
if aa>4 goto some_label
if xx==3 yy = 4
if xx==3 yy = 4 else aa = 2
Conditional jumps (``if condition goto label``) are compiled using 6502's branching instructions (such as ``bne`` and ``bcc``) so Conditional jumps (``if condition goto label``) are compiled using 6502's branching instructions (such as ``bne`` and ``bcc``) so
the rather strict limit on how *far* it can jump applies. The compiler itself can't figure this the rather strict limit on how *far* it can jump applies. The compiler itself can't figure this

View File

@ -783,19 +783,23 @@ Note: to do an indirect *JSR* to a routine with a varying address, you can use t
(which is not very efficient) or you have to write a small piece of inline assembly. (which is not very efficient) or you have to write a small piece of inline assembly.
Conditional execution if statements
^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
With the 'if' / 'else' statement you can execute code depending on the value of a condition:: With the 'if' / 'else' statement you can execute code depending on the value of a condition::
if <expression> <statements> [else <statements> ] if <expression> <statements> [else <statements> ]
where <statements> can be just a single statement for instance just a ``goto``, or it can be a block such as this:: If <statements> is just a single statement, for instance just a ``goto`` or a single assignment,
it's possible to just write the statement without any curly braces.
However if <statements> is a block of multiple statements, you'll have to enclose it in curly braces::
if <expression> { if <expression> {
<statements> <statements>
} else if <expression> {
<statements>
} else { } else {
<alternative statements> <statements>
} }
@ -807,7 +811,7 @@ itself defines on what status register bit it should branch on::
if_XX <statements> [else <statements> ] if_XX <statements> [else <statements> ]
where <statements> can be just a single statement for instance just a ``goto``, or it can be a block such as this:: where <statements> can be just a single statement or a block again::
if_XX { if_XX {
<statements> <statements>