Escolar Documentos
Profissional Documentos
Cultura Documentos
Page
221
Conditional Statement
This statement tests a condition and executes one or more other statements based on this condition. The IF Statement is the most commonly used conditional statement when there are only two possible values for the condition: either True or False.
Boolean Expression
This expression results in a Boolean value: either True or False. It can be a Boolean variable or constant, a relational expression, or a logical expression. Note that the terms, "Boolean expression" and "logical expression" can be used interchangeably for most purposes. However, we use Boolean expression when we want to emphasize that either a relational expression or a more general logical expression can be used.
The IF Statement
The IF Statement is used when you want only a specific statement to be executed if a condition is True. It can then be used to execute another statement if the condition is False. There are two ways of writing an IF statement, depending upon the situation.
IF THEN Syntax
The following syntax is used to execute a statement only if a condition is True: IF <Boolean expression> THEN <statement> A True or False condition is stated using a Boolean expression. If the Boolean expression evaluates to True, the statement following the reserved word, THEN, is executed. If the Boolean expression evaluates to False, the statement following THEN is not executed. For example, if you wanted to test an integer value to see if it was negative and if so, to make it positive, you could use the following IF statement:
IF Amount < 0 THEN Amount := -Amount;
Page
222
IFTHENELSE Syntax
Remember, there were two ways of writing an IF statement. The following syntax is used to execute one statement if a condition is True and to execute a different statement if that condition is False: IF <Boolean expression> THEN <statement 1> ELSE <statement 2> A True or False condition is stated using a Boolean expression. If the Boolean expression evaluates to True, the statement following the reserved word, THEN, is executed. If the Boolean expression evaluates to False, the statement following the reserved word, ELSE, is executed. In this type of IF statement, either of the two statements are executed, but not both. For example, if you wanted to find the unit price of an item, you might divide the total price by the quantity. However, if the quantity was zero and you did the division, it would result in an error in your code. To prevent this, you would want to test the quantity first. However, even if the quantity is zero, you still want to give the unit price a valid value. Thus, you might write:
IF Quantity <> 0 THEN UnitPrice := TotalPrice / Quantity ELSE UnitPrice := 0;
The relational expression, Quantity <> 0, is evaluated. If it is True (quantity is not equal to zero), the total price is divided by the quantity and the value assigned to the unit price. If it is False (quantity is equal to zero), the unit price is set to zero. Again, note the way that the IF statement was written above. The two statements that are optionally executed are indented by two spaces. However, the ELSE reserved word is aligned with the IF reserved word. Even though the program works the same regardless of any spaces or new lines, this is a helpful, visual cue as to which ELSE goes with which IF and even helps you to determine which condition triggers the ELSE (just look straight up to the preceding IF).
Page
223
The relational expression, Quantity = 0, is evaluated. If it is True, then the EXIT statement is executed, which skips the rest of the trigger code. If it is False, the EXIT statement is not executed and therefore the next statement executed is the assignment statement following the EXIT. Note that this assignment statement is not indented, because it is not part of the IF statement.
Page
224
Click on or "select" the second Text Box (the one whose label says "Second Value"). Change the following properties as indicated:
2nd Text Box: Caption Unit Price MinValue 0 SourceExpr UnitPrice
Page
225
Then, deselect the Add Label tool, select the Command button tool and add one more Command button to the form. Set its Caption property to "Clear".
Now, using the techniques discussed in the previous lesson, drag the Text Box and the Command button controls so that the form looks something like this.
Page
226
The three totals (TotalSales, TotalCredits and GrandTotal) are updated using a standard programming technique called "incrementing". The new value of the total is set to the current value of the total plus the new amount, called the "increment". In the above code, the new GrandTotal is set to the current GrandTotal plus the value of Result. When executed, this trigger code does the following: It first checks the Quantity. If Quantity is equal to zero, the rest of the trigger code is skipped; otherwise, Result is set to the product of UnitPrice and Quantity. The value of Result is now tested. If it is less than zero, then TotalCredits is incremented. If it is greater than or equal to zero, TotalSales is incremented. GrandTotal is incremented regardless of the value of Result. Select the Clear Command button and display the C/AL Editor again. Add the following code to the OnPush trigger:
Quantity := 0; UnitPrice := 0; Result := 0; TotalSales := 0; TotalCredits := 0; GrandTotal := 0;
When this code is executed, all of the variables are set back to zero.
Page
227
Page
228
Quantity -5
UnitPrice 2.50
Result 120.50
TotalSales 357.50
TotalCredits -7.00
GrandTotal 350.50
Note that no variables are changed here, only tested. The result is that the EXIT statement is not executed.
Quantity -5
Quantity -5
Again, no variables are changed at this time. The value of Result is being tested and the next statement run is being determined. Since Result is < 0, TotalCredits are incremented, while the statement that increments TotalSales is skipped.
Quantity -5
Quantity -5
Page
229
Page
230
Page
231
2.
3.
Page
232