If Statements
Xojo Version 2021 r3.1
Description:
An if statement is a conditional programming statement that, if proved true, performs a function or displays information. If Statements let us test conditions and perform different operations depending on the result.
There are 4 types of If Statements that we can work with
Single If Statement
Tests for a Single Condition and run or more programming statements based on the results
Basic Structure
If (condition) then
Statement(s)
End If
Example
If (rain = "true") then
lstBox.AddRow("Bring an Umbrella")
End If
If/Else Statement
Test for more than one condition and run one or more statements depending on each condition
Basic Structure
If (condition) then
Statement(s)
Else
Statement(s)
End If
Example
If (grade >= 65) then
lstBox.AddRow("Pass")
Else
lstBox.AddRow("Fail")
End If
If ElseIf Statement
Test for multiple conditions and run or more statements depending on each condition
Basic Structure
If (condition) then
Statement(s)
ElseIf (condition) then
Statement(s)
ElseIf (condition) then
Statement(s)
Else
Statement(s)
End If
Example
If (temp >= 80) then
lstBox.AddRow("It's Hot")
ElseIf (temp >= 70) then
lstBox.AddRow("It's Warm")
ElseIf (temp >= 60) then
lstBox.AddRow("It's Cool")
Else
lstBox.AddRow("It's Cold")
End If
Nested If Statement
Test for multiple conditions in a ripple effect way.
Basic Structure
If (condition 1) then
If (condition 2) then
Statement(s)
End If
Else
Statement(s)
End If
Example
If (temp >=60) then
If (temp >= 80) then
lstBox.AddRow("It's Hot")
ElseIf (temp >= 70) then
lstBox.AddRow("It's Warm")
Else
lstBox.AddRow("It's Cool")
End If
Else
lstBox.AddRow("It's Cold")
End If