 |
If Selection Construct
|
|
|
Description: |
| | An If Selection Construct conditionally executes a group of statements, depending on the value of an expression. |
Syntax: |
| | The following shows the format of the If Selection Construct syntax.
| Visual Basic | C & Java |
If <condition> Then
<statements>
[Elseif <conditionX> Then
<statementsX>]
[Else]
<elsestatements>
End If | if (<condition>)
{
<statements>
}
[else if (<conditionX>)]
{
<statementsX>
}
[else]
{
<elsestatements>
} |
| BASIC |
If <condition> Then <statements> [Else <elsestatements>] NOTE: If any statements are encountered on the same line after the "Then" keyword, then the Single-Line style syntax is used. If a carriage return is encountered after the "Then" keyword, then the Block style syntax is used. The same is true with the C/Java syntax. NOTE: Any non-zero condition result will evaluate as TRUE, a zero condition result will evaluate as FALSE. NOTE: If multiple conditions must be met, each condition needs to be enclosed in parentheses () (Examples below). |
The following table lists the elements of the If Selection Construct syntax.
| Element | Description |
| Basic | C & Java |
| <condition> | <condition> | A numeric expression that evaluates to True or False. See Relational Operators for examples of conditional constructs. |
| <statements> | <statements> | One or more statements executed if <condition> is True. |
| <conditionX> | | A numeric expression that evaluates to True or False. |
| <statementsX> | | One or more statements executed if <conditionX> is True. |
| <elsestatements> | <elsestatements> | One or more statements executed if no previous condition or is False. |
|
Operation: |
| | Basic Syntax: When a If Selection Construct is encountered during the program's execution, the condition specified by the <condition> element is evaluated. If the condition is met, the statement in the <statements> element are executed, after which, the program flow skips to the end of the If Selection Construct at which point the program resumes execution. If the condition was not met, then the program flow skips past the statement in the <statements> element, at which point, the ELSE, ELSEIF or END IF keywords are expected.If no previous condition has been met within a Blocked style If Selection Construct, and the optional ELSEIF keyword is encountered, the condition specified by the <conditionX> element is evaluated. If the condition is met, the statement in the <statementsX> element are executed, after which, the program flow skips to the end of the If Selection Construct at which point the program resumes execution. If the condition was not met, then the program flow skips past the statement in the <statementsX> element, at which point, another ELSEIF keyword or the ELSE, or END IF keywords are expected. An unlimited number of ELSEIF keywords, each with its own condition and statements, are allowed within a Blocked style If Selection Construct. If no previous condition has been met and the optional ELSE keyword is encountered, the statements in the <elsestatements> element are executed, after which, the program flow skips to the end of the If Selection Construct at which point the program resumes execution. The decision structure can be arranged in a Single-Line, or a Block style format. If the Single-Line style is used, the END IF keywords are not required, but the entire If Selection Construct must be one a single line. The style is determined by the presents of any statement after the THEN keyword. If a statement is encountered after the THEN keyword, then the compiler assumes that the Single-Line style is being used. If a carriage return is encountered after the THEN keyword, then the compiler assumes the Block style is being used. C & Java Syntax: When a If Selection Construct is encountered during the program's execution, the condition specified by the <condition> element is evaluated. If the condition is met, the statement in the <statements> element are executed, after which, the program flow skips to the end of the If Selection Construct at which point the program resumes execution. If the condition was not met, then the program flow skips past the statement in the <statements> element, at which point, else keyword is optionally expected. If the previous condition has been met and the optional else keyword is encountered, the statements in the <elsestatements> element are executed, after which, the program flow skips to the end of the If Selection Construct at which point the program resumes execution. |
Example: |
| | In the following example, an If Selection Construct is shown that checks the value of A is to see if it is equal to 1, If it is, then the statements within <statements> are executed. Notice that in the Basic syntax, the Single-Line style was selected by including statements after the Then keyword
| BASIC | C, Java |
If A = 1 Then <statements> | If (A == 1) <statements> |
In the following example, an If Selection Construct is shown that checks the value of A to see if it is more than 10, If it is, then the statements within <statements1> are executed. If it is not, then the statements within <statements2> are executed This example also shows how to construct multiple conditional comparisons.
| Visual Basic | C, Java |
If (A > 10) And (A < 20) Then
<statements1>
Else
<statements2>
End If | If ((A > 10) And (A < 20))
{
//<statements1>
}
Else
{
//<statements2>
} |
In the following example, an If Selection Construct is shown that checks the value of A to see if it is more than 10, If it is, then the statements within <statements1> are executed. If it is not, then the value of B is checked to see if it is more than 10, if it is, then the statements within <statements2> are executed. If it is not, then the statements within <statements3> are executed.
| Basic | C, Java |
If A > 10 Then
<statements1>
ElseIf B > 10 Then
<statements2>
Else
<statements3>
End If | Not possible using a single If Construct in C and Java syntax. To check multiple conditions, supply multiple If Constructs as shown.If (A.Value > 10)
{
<statements1>
}
Else
{
If (B.Value > 10)
{
<statements2>
}
Else
{
<statements3>
}
} |
In the following example, an If Selection Construct is shown that checks the value of A to see if it is equal to 10, If it is, then the statements within <statements1> are executed. If it is not, then the value of A is checked to see if it is equal to 47, if it is, then the statements within <statements2> are executed. Otherwise, nothing is done. NOTE: The Case Selection Construct is better suited for the conditions in this example.
| Basic | C, Java |
If A = 10 Then
<statements1>
ElseIf A = 47 Then
<statements2>
End If | Not possible using a single If Construct in C and Java syntax. To check multiple conditions, supply multiple If Constructs as shown.If (A == 10)
{
<statements1>
}
Else
{
If (A == 47)
{
<statements2>
}
} |
|
Related Items:
|
| | Case Selection Construct |
Version History and Bug List: |
| | Firmware Ver A1: Introduced. Bugs: No known bugs. |
|