From QB64 Wiki
IF...THEN statements make Boolean True or False program evaluations to automate program decision making.
- IF condition THEN action ELSE evaluation = 0
- The conditional evaluation by IF must be true(-1) or a non-zero numerical value for the THEN action statements to be executed.
- Multiple conditional evaluations can be made using inclusive AND or alternative OR conditional expressions.
- IF statements can also have alternative evaluations using ELSEIF and ELSE conditions.
- When the IF statement and/or code to be run is more than code line, an END IF statement MUST be used.
- With multiple code lines to run, end the IF statement with THEN and place all of the code on lines below that line.
- Multiple code line block statements require that the IF...THEN, ELSEIF, ELSE and END IF be on separate lines.
- The IDE may return an error of NEXT without FOR or LOOP without DO when END IF does not end a statement block!
- The QB64 IDE may red line the IF THEN statement line until END IF closes the statement block.
- Use colons to execute multiple statements in a one line IF statement. You cannot use AND statements after THEN!
- An underscore can be used anywhere after the code on one line to continue it to the next line in QB64 ONLY.
- NOTE: STRING values can only be evaluated in an IF statement if a value is compared to a literal or CHR$ string value. QB64 may not compile literal IF string statements or indicate an IDE coding error! Use LEN or ASC to compare strings numerically.
|<||Less than||IF a < b THEN|
|>||Greater than||IF a > b THEN|
|=||Equal||IF a = b THEN|
|<=||Less than or equal||IF a <= b THEN|
|>=||Greater than or equal||IF a >= b THEN|
|<>||NOT equal||IF a <> b THEN|
Operands Operations A B NOT B A AND B A OR B A XOR B A EQV B A IMP B T T F T T F T T T F T F T T F F F T F F T T F T F F T F F F T T
Example 1: One line IF statement
IF x > 100 THEN PRINT x
Example 2: IF statement blocks require that the IF THEN and END IF statements be separate from the code executed.
IF x > 100 THEN y = 200 PRINT y PRINT x END IF
Example 3: True or False evaluation of a numerical value executes only when the value is not 0. Cannot evaluate STRING values!
IF x THEN PRINT x
- Example will only print if a numerical value is True (positive or negative). (Equivalent to: IF x > 0 OR x < 0 THEN evaluation)
Example 4: Multiple evaluations using parenthesis to determine the order.
IF (value% > 100 AND value% < 200) OR value% = 50 THEN PRINT "OK"
Example 5: Using multiple IF options in a one line statement.
Example 6: STRING values can be compared using greater than, less than, not equal to or equal to operators only.
- Floating decimal point numerical values may not be compared as exactly the same value. QB64 will compare them the same.
- Example: Qbasic will print unequal in the IF comparison code below even though it is exactly the same value printed.
x# = 5 / 10 y# = 6 / 10 z# = x# + y# PRINT x#, y#, z# IF x# + y# = z# THEN PRINT "equal" ELSE PRINT "unequal"
- Note: QB64 will make the calculation correctly and print equal. Change older program code that relies on the error accordingly.