• Article: TA44048
• Old Article: 589
```The IF/THEN statement can give some unexpected results.  The rules for IF/THEN
are:

1. When the logical expression is true, the rest of the line is executed,
except:

A. When there is an ELSE on the same program line, execution continues to
the ELSE, then goes the next numbered program line.

B. If there is an implicit or implied GOTO, execution continues at the
target line.

2. When the logical expression is false, execution continues on the next
numbered program line, except:

A. When there is an ELSE on the line, execution continues after the ELSE.

The following examples might help explain some of these special cases:

Example 1:

10 if A = 0 then print "1" :  print "2"
20 print "line 20"

Assuming         A = 0                   A <> 0

Results            1                     line 20
2

If A equals 0, then the Print statements in line 10 are executed (rule 1).
If A doesn't equal 0, then line 20 is executed (rule 2).

Example 2:

10 if A = 0 then 500 :  print "1" :  print "2"
20 print "line 20"
500 print "end"

Assuming         A = 0                   A <> 0

Results          end                     line 20
end

If A equals 0, the Print statements in line 10 are never executed; then
execution transfers to line 500 (rule 1B).  If A doesn't equal 0, then line
20 is executed (rule 2).

Example 3:

10 if A = 0 then print "1" :  goto 500 :  print "2"
20 print "line 20"
500 print "end"

Assuming         A = 0                   A <> 0

Results          1                       line 20
end                     end

If A equals 0, then the first Print is executed, then the execution is
transfer to line 500 (rule 1A).  If A doesn't equal 0, then line 20 is
executed (rule 2).  The last Print statement in line 10 is never executed.

Example 4:

10 if A = 0 then print "1" :  print "2" :  else print "3" : print "4"
20 print "line 20"

Assuming        A = 0                   A <> 0

Results         1                       3
2                       4
line 20                 line 20

This is a classic example of how ELSE is used.  Either execute Print 1 and 2
or execute Print 3 and 4 (rules 1A, 2A).

Example 5:

10 if A = 0 then 500 : print "1" : print "2" : else print "3"
20 print "line 20"
500 print "end"

Assuming         A = 0                   A <> 0

Results          end                     3
line 20
end

Print 1 and 2 in line 10 is never executed, because if A equals 0, then
execution transfers to line 500 (rule 1B).  If A doesn't equal 0, then the
statements after the ELSE is executed (rule 2A).

Example 6:

10 if A = 0 then print "1" : goto 500 : print "2" : else print "3"
20 print "line 20"
500 print "end"

Assuming         A = 0                   A <> 0

Results          1                       3
end                     line 20
end

If A equals 0, then Print 1 is executed, then execution is transfer to line
500 (rule 1B).  If A doesn't equal 0, then the statements after the ELSE is
executed (rule 2A).  Print 2 statement is never executed.
```

Back