IF...THEN

From QB64 Wiki

Revision as of 21:18, 10 September 2011 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

IF...THEN statements make Boolean True or False program evaluations to automate program decision making.


Syntax:

IF true <> 0 THEN 'execute code statement


  • The evaluation by IF must be true(-1) or non-zero for the THEN code statements to be run.
  • 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 and END IF statements be the only ones on each line.
  • Qbasic's IDE may return an error pointing somewhere else if you forget to use END IF with a statement block!
  • Use colons to execute multiple statements in a one line IF statement. You cannot use AND on the other side of THEN!
  • An underscore can be used anywhere after the code on one line to continue it to the next line in QB64 ONLY.


Relational Operators:
  • > is Greater than
  • < is Less than
  • = is Equal to
  • >= is Greater than or Equal to
  • <= is Less than or Equal to
  • <> is Not Equal to
When evaluating a number value, no IF value > 0 operation is necessary for values not 0. Use: IF value THEN


Boolean Conditional Operators:
  • AND (boolean) can be used to add extra conditions to a boolean statement evaluation.
  • OR (boolean) can be used to add alternate conditions to a boolean statement evaluation.
  • Parenthesis are allowed inside of boolean statements to clarify an evaluation.


Mathematical Logical operators:
* Truth table of the 6 BASIC Logical Operators:


The results of the bitwise logical operations, where A and B are operands, and T and F indicate that a bit is set or not set:
Operands Operations
A B NOT B A AND B A OR BA XOR BA EQV BA 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
Note that the Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively. This allows relational tests to be inverted and combined using the bitwise logical operations.


* Note that Basic returns -1 for True and 0 for False.


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.

x = 100 'change x value to see other evaluations IF x > 200 THEN PRINT "High" ELSEIF x < 0 THEN PRINT "Low" ELSE PRINT "OK"


Example 6: STRING values can be compared using greater than, less than, not equal to or equal to operators only.

PRINT "Press a letter key: "; Key$ = INPUT$(1) PRINT Key$ IF Key$ >= CHR$(65) AND Key$ <= CHR$(90) THEN PRINT "A to Z"

Explanation: Long STRING expression values are compared by their cumulative ASCII code values.


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page