Conditions and Loops: if-else and while. Source Dynamics Source Insight 4.0
Annons
Annons
Conditions and Loops: if-else and while
Operator
>
<=
>=
#
"@var@"
Table 5.3: Operators
Meaning
greater than less than or equal to greater than or equal to infix string concatenation - eg:
"dog" # "cat" variable expansion - used inside of quoted strings to expand a variable in the string.
Since variables may contain non-numeric values, relational operators are treated thus:
Operator
==
!=
<
>
<=
>=
Table 5.4: Relational Operators for Strings
Meaning
strings must be equal (case sensitive) strings must not be equal (case sensitive) strings are converted to numbers and then compared.
Empty strings or strings that are non-numeric result in a runtime error.
Conditions and Loops: if-else and while
The Source Insight macro language supports if
and while
statements for conditional tests and looping.
The if Statement
The if
statement is used to test a condition and execute a statement or statements based on the condition.
The if
statement has the following syntax: if (condition)
statement // condition is true
In the above example, if condition is true, then statement is executed. You can use
{}
brackets to group more than one statement. For example: if (condition)
{
statement1
statement2
}
An else clause can be added to the if
statement: if (condition)
statement1 // condition is true else
statement2 // condition is false
Note:
“Conditional Evaluation” on page 377.
376 Source Insight 4 User Guide
Conditions and Loops: if-else and while
The while statement
The while
statement loops while a condition is true. The while
statement has the following syntax: while (condition)
statement // keeps executing statement until condition is false
In the above example, the statement in the while block is executed as long as the condition is true. The condition is tested at the top of the loop each time. You can use
{}
brackets to group more than one statement. For example: while (condition)
{
statement1
statement2
}
Note:
“Conditional Evaluation” on page 377.
Break and Continue
The break
statement is used to exit out of a while loop. If more then one while loop is nested, the break statement breaks out of the innermost loop.
while (condition)
{
if (should_exit)
break // exit the while loop
…
}
The continue
statement continues again at the top of the loop, just before condition expression is evaluated.
while (condition)
{
if (skip_rest_loop)
continue // continue at the top of the while loop
…
}
Conditional Evaluation
Source Insight evaluates the whole conditional expression each time. Expressions are not short-circuited.
This is an important difference from the way that C conditional expressions are evaluated. In C, the expression may be partially evaluated. Consider the following conditional expression: if (hbuf != hNil && GetBufLineCount(hbuf) > x)
This conditional expression would lead to an error in Source Insight if hbuf
is equal to hNil
. In C, the evaluation would be terminated after determining that hbuf != hNil
. In Source Insight, the whole expression is evaluated. In this case, causing an error since hNil
would be passed as the buffer handle to GetBufLine-
Count.
Source Insight 4 User Guide 377
Ladda ner
Annons