If-Then-Elif-Else

If <expression> Then <statement>(s) [Else statement(s)]

If <expression> Then

[statement(s)]

[Elif <expression> Then]

[statement(s)]

...

[Else]

[statement(s)]

End If

Makes a decision regarding program flow based on the result returned by an expression.

If the result of <expression> is nonzero (logical true), the Then statement(s) is executed.

If the result of expression is zero (false), the Then statement(s) is ignored and the Else statement(s), if present, is executed.

Elif statement, as its name suggests, is a combination of If and Else.

Like Else, it extends an If statement to execute a different statement in case the original If expression evaluates to False. However, unlike Else, it will execute that alternative expression only if the Elif conditional expression evaluates to True.

Otherwise, execution continues with the next executable statement.

Example 1:

If N%<20 And N%>10 Then Print "OK": GoTo skip_error

Print "N% IS OUT OF RANGE"

skip_error:

Rem --- N% is in the range (10..20)

Example 2:

If x>0 Then Print "Positive" Else Print "Negative"

Example 3:

If x>0 Then

Print "Positive"

ElIf x<0 Then

Print "Negative"

Else

Print "Zero"

End If

Example 4:

' If A > 10, execute the three colon-separated statements in the order

' that they appear

If A > 10 Then A = A + 1 : B = B + A : C = C + B

Example 5:

' If A > 10, execute the three statements in the order

' that they appear

If A > 10 Then

A = A + 1

B = B + A

C = C + B

End If

Example 6:

Input n%

If n% mod 2 = 0 Then

Print n%; " is even"

Else

Print n%; " is odd"

End If

Example 7:

' graph2.bas - This file is part of nuBASIC

dx%=GetSWidth()

dy%=GetSHeight()

Randomize

While 1

x1%=Rnd(1)*dx%

x2%=Rnd(1)*dx%

y1%=Rnd(1)*dy%

y2%=Rnd(1)*dy%

t%=Int(Rnd(1)*6)

c%=Int(Rnd(1)*&hffffff)

If t%=1 Then

Line x1%,y1%,x2%,y2%,c%

Elif t%=2 Then

Rect x1%,y1%,x2%,y2%,c%

Elif t%=3 Then

FillRect x1%,y1%,x2%,y2%,c%

Elif t%=4 Then

Ellipse x1%,y1%,x2%,y2%,c%

Else

TextOut x1%,y1%, "nuBASIC",c%

End if

k$=InKey$()

If k$="q" Then End

End While