BB___IF_THEN

=IF..THEN..ELSE..END IF= toc

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 true//THEN >>> //do one thing// >> ELSE >>> //do another thing// >> END IF //evaluating is done//

> 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

 * 1) if //test expr// then //statement//
 * 2) if //test expr// goto //[branchLabel]//
 * 3) if //test expr// then //statement// else //statement//

Conditional block form
>> //statement[s]// > end if >> //statement[s]// > else >> //statement[s]// > end if
 * 1) if //test expr// then
 * 1) if //test expr// then

Nested
>> if //expr// then >>> //statement[s]// >> end if > else >> //statement[s]// > end if
 * 1) if //test expr// then

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]

Single-line statements
code format="lb" ' 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 code

Conditional block form
code format="lb" ' 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 code

Additional examples
code format="lb" ' 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. code

Useful Functions
Inline IF functions. code format="lb" '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 resrict 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 code