Control Structures


Control structures are used within JScript to control the flow and execution of your scripts. A number of different structures are available, including if…then…else, do…while, and try…catch statements.

If…Then…Else

If…then…else statements are constructed by placing the statement to be evaluated in parentheses, followed by the script that is to be executed if the statement is true, as shown in the following example:

if (InvoiceAmount > 10000)
XDocument.UI.Alert("Please use a different form");
else
XDocument.UI.Alert("This is the correct form");

If you have multiple lines that are to be executed on the true condition, then you can use curly braces to surround these lines, as shown here:

if (InvoiceAmount > 10000)
{
XDocument.UI.Alert("Please use a different form");
XDocument.UI.Alert("This is the wrong form");
}
else
XDocument.UI.Alert("This is the correct form");

Do…While

Another control structure used in JScript is the do…while loop, which will execute a section of script as long as the while condition is met. In the following example, a counter variable (i) is incremented by one and displayed each time through the loop until it reaches 5:

   var i = 0;
while (i < 5)
{
i++;
XDocument.UI.Alert(i);
}

Try…Catch

Try…catch statements are designed to catch errors, with a section of code to “try” or run and then a section of code to be run when an exception occurs. In the following example, the try statement is trying to perform a calculation—if an error occurs in the calculation, an alert will be displayed with the text shown:

try { cost = totalprice/quantity }
catch(e)
{ XDocument.UI.Alert("There was an error!");}

Note

For more information on other types of control structures within JScript, check out the JScript reference found in the Microsoft Script Editor by selecting Help | Microsoft Script Editor Help.




How to Do Everything with Microsoft Office InfoPath 2003
How to Do Everything with Microsoft Office InfoPath 2003 (How to Do Everything)
ISBN: 0072231270
EAN: 2147483647
Year: 2006
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net