See Also: Boolean Expressions, Compound Statements, Conditional Statements, Continuing a Statement on the next line, Execution Control Commands, Begin, Case, If Function
Conditionally controls the execution of one or more lines of code.
If {BooleanExpression} {TrueStatement}
[ Else {FalseStatement} ]
or
If {BooleanExpression} Begin
{TrueStatement}
End
[
Else Begin
{FalseStatement}
End
]
The If command evaluates the Boolean expression provided, and if the result is True, the succeeding line or block of code executes. If the Boolean expression is False, program control changes to the statement immediately after the true conditional code, and if that statement is an Else statement, the false conditional code that follows the Else command will execute.
You can use a Begin … End block to designate a block of code that will conditionally execute.
Procedure Example
Boolean bFileExists
// File_Exist returns non-zero if the file in question exists.
File_Exist "hello.txt" bFileExists
// If the file exists, put up a message box that says it was found.
If (bFileExists);
Send Stop_Box "Hello.Txt was found" "Test!"
End_Procedure // Example
The next example shows how the Else clause is used when the Boolean expression is FALSE:
Procedure Example
Boolean bFileExists
// File_Exist returns non-zero if the file in question exists.
File_Exist "hello.txt" bFileExists
// If the file exists, put up a message box that says it was found.
If (bFileExists);
Send Stop_Box "Hello.Txt was found" "Test!"
Else Begin // The file does not exist. Tell the user we cannot find it.
Send Stop_Box "Hello.Txt was not found" "Test!"
Procedure_Return
End
// do more stuff
End_Procedure // Example
This example uses a Begin…End block for both the If and Else conditions:
Procedure Example
Boolean bFileExists
File_Exist "hello.txt" bFileExists
If (bFileExists) Begin
Send Stop_Box "Hello.Txt was found" "Test!"
// Erase Hello.Txt so that the next time it is not found.
EraseFile "hello.txt"
End
Else Begin
Send Stop_Box "Hello.Txt was not found." "Test!"
// Create Hello.Txt so that the next time it is found.
Direct_Output "hello.txt"
WriteLn "Hello World"
Close_Output
End
End_Procedure // Example
{BooleanExpression} can be any expression that evaluates to True or False.
This example searches an array of structs of type tFriends for a friend's birthday and compares it to today's date. If today is any friend's birthday, it displays a message box stating "Happy Birthday " and the friend's name.
Struct tFriends
String sName
String sPhoneNumber
Date dBirthday
End_Struct
Procedure Example
tFriends[] myFriends
Date dToday
Integer i iNumberOfFriends
SysDate dToday
// add some code here to initialize array myFriends
Move (SizeOfArray(myFriends)) to iNumberOfFriends
For i from 0 to (iNumberOfFriends-1)
If (myFriends[i].dBirthday = dToday) ;
Send Info_Box ("Happy Birthday " + myFriends[i].sName)
Loop
End_Procedure // Example
Note: normally, you would use the SearchArray function to find an array element.
You may use Not to invert a conditional test:
Procedure Example
Boolean bFileExists
File_Exist "hello.txt" bFileExists
If (Not(bFileExists)) Begin
Send Stop_Box "Hello.Txt was not found." "Test!"
// Create Hello.Txt so that the next time it is found.
Direct_Output "hello.txt"
WriteLn "Hello World"
Close_Output
End
End_Procedure // Example
Else commands are paired with the preceding If command. Use indentation to make your code readable. Here is an example that prints one of four message boxes, depending on whether the file "hello.txt" exists, is empty, has one line, or has more than one line:
Procedure Example
Boolean bFileExists
String sBuffer
Direct_Input "hello.txt"
// If the file exists, SeqEof will be FALSE (0)
If (Not(SeqEof)) Begin
Readln sBuffer
// If SeqEof is set, the file is empty. Tell the user.
If (SeqEof) Send Stop_Box "Hello.Txt exists, but is empty" "Test!"
Else Begin // At least one line of text in the file.
Readln sBuffer
If (SeqEof); // At least two lines of text in the file.
Send Stop_Box "Found two lines of text in Hello.Txt"
Else ; // Just one line of text in the file.
Send Stop_Box "Found one line of text in Hello.Txt"
End
Close_Input
EraseFile "hello.txt"
End
Else Begin // If the file doesn't exist…
Send Stop_Box "Hello.Txt was not found." "Test!"
Direct_Output "hello.txt"
WriteLn "Hello World"
Close_Output
End
End_Procedure // Example
Notice that when you do not use a Begin…End construct, it is crucial to use a semi-colon continuation character ';' to execute the line after the If command. A semi-colon is not necessary if the command to conditionally execute is on the same line as the If command, such as in the test for an empty file above.
An Integer variable or expression with a value of zero (0) is equivalent of FALSE. An Integer or expression with a value other than zero is the equivalent of TRUE.