GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | IF

Edit History

IF

IF expr

Home > Reference > Language

Tests the expression and if it evaluates to a non-zero value, program flow will resume after the following THEN statement.

Block-style IF

IF expression1 [THEN]
    .
    . [commands]
    .
[ [ELSEIF| ELIF] expression2 [THEN]
    .
    . [commands]
    .
]
[ELSE
    .
    . [commands]
    .
]
ENDIF| FI

Causes SmallBASIC to make a decision based on the value of an expression.

  • expression - An expression; 0 is equivalent to FALSE, while all other values are equivalent to TRUE.
  • commands - One or more commands.

Each expression in the IF/ELSEIF construct is tested in order. As soon as an expression is found to be TRUE, then its corresponding commands are executed. If no expressions are TRUE, then the commands following the ELSE keyword are executed. If ELSE is not specified, then execution continues with the command following the ENDIF.

IF, ELSE, ELSEIF, and ENDIF must all be the first keywords on their respective lines.

THEN is optional, but if its defined it must be the last keyword on its line; if anything other than a comment follows on the same line with THEN, BASIC thinks it’s reading a single-line IF/THEN/ELSE construct. IF blocks may be nested.

Single-line IF

IF expression THEN [num-label]|[command] [ELSE [num-label]|[command]]

Causes SmallBASIC to make a decision based on the value of an expression.

  • expression - An expression; 0 is equivalent to FALSE, while all other values are equivalent to TRUE.
  • command - Any legal command or a numeric label. If a number is specified, it is equivalent to a GOTO command with the specified numeric-label.

Example 1: Block-style IF

x = 1
IF x = 1 THEN
    PRINT "true"
ELSE
    PRINT "false"
ENDIF

' Alternate syntax:
x = 1
IF x = 1
    PRINT "true"
ELSE
    PRINT "false"
FI

Example 2: Single-line IF

x = 1
IF x = 1 THEN PRINT "true" ELSE PRINT "false"

IF x = 1 THEN 1000

end ' ends the program

1000 PRINT "true"

Example 3: Nested IF

x = 1
y = 2

IF x == 1 THEN 
    PRINT "x equals 1"
    
    if y == 2 THEN
        PRINT "y equals 2"
    ELSE
        PRINT "y not 2"
    ENDIF
    
ELSE
    PRINT "x not 1"
ENDIF

Example 4: IF … ELSEIF … ELSE

foo = 1     ' change value to 1,2,3

IF foo == 1
    PRINT "one"
ELSEIF foo == 2
    PRINT "two"
ELSE
    PRINT "something else"
ENDIF
Code samples using IF
001 3 ways to print hello 5 times.bas   002 numeric variables.bas   003 conditional branching.bas   004 loops.bas   005 challenge.bas   006 arrays+.bas   100lines.bas   2048.bas   3d block lettes.bas   3d rotating cube with message.bas   3d wire cube v1.bas   3d wire cube.bas   3d_palmx.bas   3d_torus.bas   3dtictac.bas   3dtorus.bas   3dttt.bas   3dttt.bas   4tune.bas   7gables.bas   A Rubens, Peter Paul Landscape.bas   agendus.bas   ai.bas   amortig.bas   analog clock.bas   anball 1.0.bas   angel feather.bas   anomail.bas   Another center finder.bas  
Language
AND   AS   BAND   BG   BOR   BYREF   CALL   CASE   CATCH   CONST   DECLARE   DEF   DO   ELIF   ELSE   ELSEIF   END   END TRY   ENDIF   EQV   EXIT   FALSE   FI   FOR   FUNC   GOSUB   GOTO   IF   IFF   IMP   IN   LABEL   LET   LIKE   LOCAL   LSHIFT   MDL   MOD   NAND   NEXT   NOR   NOT   ON   OR   REM   REPEAT   RETURN   RSHIFT   SELECT   STEP   STOP   SUB   THEN   THROW   TO   TRUE   TRY   UNTIL   USE   USG   USING   WEND   WHILE   XNOR   XOR  
This page was last edited on Fri, 24 Mar 2023 13:19:57 +0100 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.