Monday, July 27, 2009

WITH Statements

Following is the Syntax For WITH statment.
WITH [Record] DO [Statement]
When you work with records, addressing is created as record name, dot (period),
and field name: [Record].[Field]
you work continuously with the same record, you can use WITH statements. When you use a WITH statement, you only have to specify the record name once.
Within the scope of [Statement], fields in [Record] can be addressed without having to specify the record name.
You can nest several WITH statements. In case of identical names, the inner WITH overrules the outer WITH-statements.
For Eg:
Consider a variable-'CustRec', Data Type -'Record', Subtype-'Customer'.
CustRec."No." := '10000';
CustRec.Name := 'Creative Solutions';
CustRec."Phone No." := '555-444-333';
CustRec.Address := '1352 Avenue';
CustRec.City := 'NewYork City';
MESSAGE('A variable has been created for this customer.');
The following example shows another way to create a record variable that you can commit later.
WITH CustomerRec DO
BEGIN
"No." := '10000';
Name := 'Creative Solutions';
"Phone No." := '555-444-333';
Address := '1352 Avenue';
City := 'NewYork City';
MESSAGE('A variable has been created for this customer.');
END;

Saturday, July 25, 2009

FOR,WHILE,REPEAT

C/AL Repetitive Statements
A repetitive statement is also known as a loop. The Different Loop statements are Given below.
1. FOR
2. WHILE
3. REPEAT

FOR
Repeats the inner statement until a counter variable equals the maximum or minimum value specified.
The Two Control Structures In FOR Statement are FOR TO and FOR DOWNTO.
The Syntax For The above control structures are given below.
FOR [Control Variable] := [Start Number] TO [End Number] DO
[Statement]
The data type of [Control Variable], [Start Number], and [End Number] must be Boolean, number, time, or date.
Use FOR statements when you want to execute code a specific number of times.
Use a control variable to control the number of times the code is executed. The control variable can be increased or decreased by one, depending on whether TO or DOWNTO is used.
When using a FOR TO loop, the [Statement] will not be executed if the [Start Number] is greater than the end value. Likewise, when using a FOR DOWNTO loop, the [Statement] will not be executed if the [Start Number] is less than the end value.
For Eg:
1. Consider a variable 'I' for Loop.
FOR I=1 To 5 DO
Statement 1;
2. The following example shows how to nest FOR statements.
FOR I := 1 TO 5 DO
FOR J := 1 TO 7 DO
A[I,J] := 23;

Thursday, July 23, 2009

IF THEN ELSE in Navision

IF THEN [ELSE]
A conditional statement is one type of control structure in C/AL.
Using Conditional Statements
By using a conditional statement, you can specify a condition and one or more commands that should be executed, according to whether the condition is evaluated as TRUE or FALSE. There are two types of conditional statements in C/AL:
1. IF THEN [ELSE], where there are two choices
2. CASE, where there are more than two choices.
IF THEN ELSE Statements
IF THEN ELSE statements have the following syntax.
IF (Condition) THEN (Statement1)
[
ELSE
(Statement2)
]
This means that if (Condition) is true, then (Statement1) is executed. If (Condition) is false, then (Statement2) is executed.
The square brackets around ELSE mean that this part of the statement is optional. The ELSE statement is used when different actions are executed, depending on the evaluation of (Condition).
You can build even more complex control structures by nesting IF THEN ELSE statements. The following example is a typical IF THEN ELSE statement.
IF (Condition1) THEN
IF (Condition2) THEN
Statement1;
ELSE
Statement2
Note: A semicolon preceding an ELSE is not allowed.
If 'Condition1' is false, then nothing is executed. If 'Condition1' and 'Condition2' are both true, then 'Statement1' is executed. If 'Condition1' is true and 'Condition2' is false, then 'Statement2' is executed.
Reading several nested IF THEN ELSE statements can be quite confusing but a general rule is that an ELSE belongs to the last IF that lacks an ELSE.
Example 1
The following example shows an IF statement without the optional ELSE part.
IF Amount (<) 1000 THEN Total := Total + Amount;
Example 2
The following example shows an IF statement with the optional ELSE part.
(1)...
(2) IF Amount (<1000) align="justify">(3) THEN BEGIN
(4) IF I > J THEN Max := I
(5) ELSE Max := J;
(6) Amount := Amount * Max;
(6) END
(7) ELSE
(8)...
A common error is to put an extraneous semicolon at the end of a line before an ELSE (line 4). As mentioned earlier, this is not valid according to the syntax of C/AL. The semicolon is used as a statement separator. (The end of line 4 is inside the inner IF statement.)
CASE Statements
CASE statements have the following syntax.
CASE (Expression) OF
(Value set 1) : (Statement 1);
(Value set 2) :(Statement 2);......
(Value set n) : (Statement n);
[ ELSE (Statement n+1) ]
CASE statements are also called multiple option statements and are typically used when you must choose between more than two different actions.
The function of the CASE statement is as follows:
1. The 'Expression' is evaluated, and the first matching value set executes the associated statement, if there is one.
2. If none of the value sets matches the value of the expression, and the optional ELSE part has been omitted, no action is taken. If the optional ELSE part is used, then the associated statement is executed.
The data type of the value sets must be the same as the data type of 'Expression' or at least be convertible to the same data type.
For Eg:
The following C/AL code prints various messages depending on the value of Number. If the value of Number does not match any of the entries in the CASE structure, the ELSE entry is used as the default.
CASE Number OF
1,2,9: MESSAGE('1, 2, or 9.');
10..100: MESSAGE('In the range from 10 to 100.');
ELSE MESSAGE('Neither 1, 2, 9, nor in the range from 10 to 100.');
END

BEGIN END Statements

A compound statement is one type of control structure in C/AL.
Using Compound Statements, In some cases, the C/AL syntax only allows you to use a single statement.
If however you have to execute more than one simple statement, the statements can be written as a compound statement by enclosing the statements between the keywords BEGIN and END.
BEGIN
Statement 1;
Statement 2;
.. ;
END

The individual statements are separated by a semicolon. In C/AL a semicolon is used to separate statements, and not, as in other programming languages, as a terminator symbol for a statement. Nevertheless, an extra semicolon before an END does not cause an error because it is interpreted by the compiler as an empty statement.
Blocks
The BEGIN-END structure is also called a block. Blocks can be very useful in connection with the other control structures in C/AL

Tuesday, July 21, 2009

C/AL Control Structures

All the C/AL programs that you create consist of one or more statements, which are executed sequentially in top-down order. However, you will often need to control the direct top-down flow of the execution. One or more statements may have to be repeated more than once, or you may have to make the execution of a certain statement conditional. To do this, you use control structures.

The control structures in C/AL are divided into the following main groups:

 1. Compound statements

 2. Conditional statements

 3. Repetitive statements

 4. WITH statements