Lingo Dictionary > G-K > if

 

if

Syntax

if logicalExpression then statement
if logicalExpression then statement 
else statement 
end if
if logicalExpression then
		statement(s) 
end if
if logicalExpression then
		statement(s) 
else
		statement(s) 
end if
if logicalExpression1 then
		statement(s) 
else if logicalExpression2 then
		statement(s) 
else if logicalExpression3 then
		statement(s) 
end if
if logicalExpression1 then
		statement(s) 
else logicalExpression2
end if

Description

Keyword; if...then structure that evaluates the logical expression specified by logicalExpression.

If the condition is TRUE, Lingo executes the statement(s) that follow then.

If the condition is FALSE, Lingo executes the statement(s) following else. If no statements follow else, Lingo exits the if...then structure.

All parts of the condition must be evaluated; execution does not stop at the first condition that is met or not met. Thus, faster code may be created by nesting if...then statements on separate lines instead of placing them all on the first line to be evaluated.

When the condition is a property, Lingo automatically checks whether the property is TRUE. You don't need to explicitly add the phrase = TRUE after the property.

The else portion of the statement is optional. To use more than one then-statement or else-statement, you must end with the form end if.

The else portion always corresponds to the previous if statement; thus, sometimes you must include an else nothing statement to associate an else keyword with the proper if keyword.

Note: A quick way to determine in the script window if a script is paired properly is to press Tab. This forces Director to check the open Script window and show the indentation for the contents. Any mismatches will be immediately apparent.

Example

This statement checks whether the carriage return was pressed and then continues if it was:

if the key = RETURN then go the frame + 1

Example

This handler checks whether the Command and Q keys were pressed simultaneously and, if so, executes the subsequent statements:

on keyDown
	if (the commandDown) and (the key = "q") then
		cleanUp
		quit
	end if
end keyDown

Example

Compare the following two constructions and the performance results. The first construction evaluates both conditions, and so must determine the time measurement, which may take a while. The second construction evaluates the first condition; the second condition is checked only if the first condition is TRUE.

spriteUnderCursor = rollOver()
if (spriteUnderCursor > 25) AND MeasureTimeSinceIStarted() then
	alert "You found the hidden treasure!"
end if

The alternate, and faster construction would be:

spriteUnderCursor = rollOver()
if (spriteUnderCursor > 25) then
	if MeasureTimeSinceIStarted() then
		alert "You found the hidden treasure!"
	end if
end if

See also

case