2. The IF Statement
Use the IF statement to control if a part of the program
executes. If the condition is TRUE, one set of one or
more statement executes; and if the condition is
FALSE, another set of one or more statements executes
IF <Boolean expression> THEN <statement>
IF Amount < 0 THEN
Amount := -Amount;
3. IF - THEN - ELSE
IF <Boolean expression> THEN
<statement 1>
ELSE
<statement 2>
IF Quantity <> 0 THEN
UnitPrice := TotalPrice / Quantity
ELSE
UnitPrice := 0;
;
4. The EXIT Statement
Code in a trigger executes sequentially from the top to
the bottom
IF Quantity = 0 THEN
EXIT;
UnitPrice := TotalPrice / Quantity;
5. Code Indentation
To indent several lines of code, select the lines, and
then press TAB.
To indent several lines of code to the left, select those
lines, and press SHIFT+TAB
6. The CASE Statement
Use the CASE conditional statement when there are
more than two possible values for the condition.
CASE <expression> OF
<value set 1> : <statement 1>;
<value set 2> : <statement 2>;
<value set n> : <statement n>;
[ELSE <statement n+1>]
END
7. CASE Example
Number:=25;
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;
Output: In the range from 10 to 100
8. Compound Statements and Comments
A compound statement is multiple statements
A comment is a description that developers write in
their code .
// this code comment
{
line 1
line 2
line 3
}
9. The Syntax of Compound Statements
BEGIN <statement> {; <statement>} END
IF Quantity <> 0 THEN
BEGIN
UnitPrice := ExtendedPrice / Quantity;
TotalPrice := TotalPrice + ExtendedPrice;
END;
10. IF Quantity <> 0 THEN
BEGIN
UnitPrice := ExtendedPrice / Quantity;
TotalPrice := TotalPrice + ExtendedPrice;
END //No ;
ELSE
BEGIN
UnitPrice := 0;
EXIT;
END; // ;
11. Compound Statement by Using Nested IF Statements
IF Amount <> 0 THEN
IF Amount > 0 THEN
Sales := Sales + Amount
ELSE
IF Reason = Reason::Return THEN
IF ReasonForReturn = ReasonForReturn::Defective THEN
Refund := Refund + Amount
ELSE
Credits := Credits + Amount
ELSE
Sales := Sales - Amount;
12. To have organized code
Use correct indentation.
Use meaningful variable names
Do not use negative variable names (for example,
DoNotDelete
Use Booleans to designate Yes or No choices.
13. Demonstrations
Demonstration: Use the Conditional and Compound
Statements in a Page (Page 12)
Practice: Nested IF (Page 21)
Lab 6.1: Use Conditional and Compound Statements
(Page 23)
14. Arrays
Arrays are complex variables that contain a group of
variables with the same data type.
Simple data types only have a single value.
A complex data type has multiple values.
Number array with 10 elements
10 5 4 50 21 63 20 12 42 25
16. How to clear Variable
CLEAR(<variable>);
For example, for a one-dimensional array named
SaleAmount of type Decimal, use the following
function call to set all elements in SaleAmount to zero
CLEAR(SaleAmount);
18. ARRAYLEN Function
In your array you have seven elements but you are
trying to access 8th index Then you will get a run time
error .
Result := ARRAYLEN(<array variable>)
ARRAYLEN(SaleAmount)
IF Counter <= ARRAYLEN(SaleAmount) THEN
SaleAmount[Counter] := Answer;
19. Strings as Arrays of Characters
Str := ‘Walk in the park’;
Str[13] := ‘d’;
MESSAGE(Str);
Output :Walk in the dark
20. Repetitive Statements
A repetitive statement is a statement that enables execution
of one or more statements multiple times.
The FOR Statement
FOR <control variable> := <start value> TO <end value> DO
<statement>
FOR idx := 4 TO 8 DO
Total := Total + 2.5;
FOR <control variable> := <start value> DOWNTO <end value> DO
<statement>
FOR Number:= 10 DOWNTO 5 DO
MESSAGE(FORMAT(Number));
21. The WHILE...DO Statement
WHILE <Boolean expression> DO <statement>
When Boolean Expression is true statement is
executed. And if the expression is False statement
skipped.
22. The REPEAT...UNTIL Statement
Use the REPEAT statement when one or more statements
are to be executed until some condition becomes true.
REPEAT
<statement> { ; <statement> }
UNTIL <Boolean expression>
Number :=10;
REPEAT
MESSAGE(FORMAT(Number));
Number:=Number-1;
UNTIL Number=2.;
23. WITH Statement
When you work with records, addressing is created as
record name, dot (period), and field name:
<Record>.<Field>
If you work continuously with the same record, then
you can use WITH statements. When you use a WITH
statement, you only specify the record name once.
24. WITH Statement
CustomerRec."No." := '1234';
CustomerRec.Name := 'Windy City Solutions';
CustomerRec."Phone No." := '555-444-333';
CustomerRec.Address := '1241 Druid Avenue';
CustomerRec.City := 'Windy City'; MESSAGE('A variablehas been created for this
customer.');
WITH CustomerRec DO BEGIN
"No." := '1234';
Name := 'Windy City Solutions';
"Phone No." := '555-444-333';
Address := '1241 Druid Avenue';
City := 'Windy City'; MESSAGE('A variable has been created for this customer.');
END;