IF..THEN
Jump to navigation
Jump to search
![]() |
![]() |
![]() |
![]() |
Description
- This is a conditional decision-making structure. Based on if the condition evaluates to true (nonzero), or false (zero), it controls which statement the program executes next, which can be a branch label or one or more additional commands or statements.
- In its simplest form, IF and THEN is a complete conditional statement. Adding the ELSE keyword extends its usefulness.
- The logic is as follows:
- IF a condition is trueTHEN
- do one thing
- ELSE
- do another thing
- END IF evaluating is done
- IF a condition is trueTHEN
- An IF..THEN..ELSE clause can be written on one line, but is usually written in blocks as above. When used in a block, use END IF to end the structure.
- IF..THEN..ELSE clauses can be nested to any level.
- See also, SELECT..CASE.
Syntax
- In all cases, 'statement' can be a branch label.
Single-line statements
- if test expr then statement
- if test expr goto [branchLabel]
- if test expr then statement else statement
Conditional block form
- if test expr then
- statement[s]
- end if
- if test expr then
- statement[s]
- else
- statement[s]
- end if
Nested
- if test expr then
- if expr then
- statement[s]
- end if
- if expr then
- else
- statement[s]
- end if
Hints
- In JustBASIC, integer nonzero evaluates to true, zero is false.
- Relational operators return 1 for true, NOT(0) returns -1.
- AND, OR, XOR are bitwise operators.
- For the details, see Boolean (logical) operators
- If the statement is a simple if..then statement, and the test expression evaluates to true, then the code following the THEN command is executed; otherwise, the next statement to be executed is the line following the if..then statement.
- Instead of single statement, one can use several statements delimited with : (colon)
- statement1:statement2:statement3 etc. as needed
- If the statement is a more complex "code block", the statements which can be executed are coded between the IF and END IF. If test expression is true, the line(s) following the IF statement is executed. If the expression is false, and an ELSE keyword is included, the line(s) following the ELSE is executed; otherwise, END IF is executed and the program continues with the statement following END IF.
- Often, the next code to be executed is a branch label, which tells the program to branch to another location in the program. If this is the case, GOTO can be used in place of THEN. These statements produce the same result:
- if answer is "yes" then [correctLabel]
- if answer is "yes" goto [correctLabel]
Example
Single-line statements
' A simple IF...THEN example ' The condition obviously evaluates to "true" ' so the statement after THEN is executed and ' "yes" is assigned to string variable answer$ if 1+1=2 then answer$ = "yes" print answer$ ' yes is printed ' If the statement is false, this happens: if 1+1=3 then ans$ = "yes" print ans$ ' yes was not printed. ' Adding ELSE to handle what to do if expression is false if 1+1=3 then ans$ = "YES" else ans$ = "NO" print ans$ ' NO was printed ' Branching as a result of IF...THEN if 1+1=2 goto [printYes] wait [printYes] print "Yes" ' Yes is printed ' Conditional branching if 1+1=3 then [printYES] else [printNO] wait [printYES] print "YES" wait [printNO] print "NO" ' NO is printed
Conditional block form
' A simple IF...THEN block ' When formed in a block, more than one ' statement can be executed. ' Use the END keyword to end the IF statement. if 1+1=2 then answer$ = "yes" print answer$ end if ' yes is printed ' If the condition is false answer$ = "no" if 1+1=3 then answer$ = "yes" end if print answer$ ' no is printed ' Adding ELSE lets us handle yes or no in one statement if 1+1=3 then answer$ = "YES" else answer$ = "NO" end if print answer$ 'NO is printed ' Branching if 1+1=3 then goto [printYESYES] else goto [printNONO] end if [printYESYES] print "YES" wait [printNONO] print "NO" ' NO is printed
Additional examples
' Note: See "File Operations" for an explanation ' of this user function, "fileExists". if fileExists(DefaultDir$, "crazytext.txt") then print "This file is existing." else print "This file does not exist." end if dim info$(10, 10) function fileExists(path$, filename$) files path$, filename$, info$() fileExists = val(info$(0, 0)) end function ' A nested IF...THEN example if 1+1>1 then if 1+1<>3 then if 1+1<3 then print "1 + 1 = 2" end if end if else print "Not sure what 1 + 1 is???" end if ' prints "1 + 1 = 2" ' Change the test expression to 1+0>1 and run the example ' Since the expression is no longer true, ' the ELSE clause is executed.
Useful Procedures
Inline IF functions.
'example: print iif(2>3, 10,20) 'Numeric result. Check fails - Prints 20 print iif$(2<3, "aaa","bbb") 'String results. Check holds - Prints aaa 'some more examples x = rnd(1)-0.5 y = sqr(iif(x>0,x,0)) 'in the expression we restrict argument to be >=0 print x, y print "X is ";iif$(x>=0, "positive","negative") '------------------------------------------------ function iif(test, valYes, valNo) iif = valNo if test then iif = valYes end function function iif$(test, valYes$, valNo$) iif$ = valNo$ if test then iif$ = valYes$ end function