Continued with control statements- elseif, case , exit and do statements and their examples.
Using elseif
We use elseif to avoid nesting ifs. Nesting ifs can be difficult to read and maintain.an example below shows the construct.
Example:
Using ELSEIF Is Clearer than Using Nested IFs
Output:1 report ztx1002. 2 parameters: f1 default 'A', 3 f2 default 'B', 4 f3 default 'C'. 5 6 if f1 = f2. write: / f1, '=', f2. 7 elseif f1 = f3. write: / f1, '=', f3. 8 elseif f2 = f3. write: / f2, '=', f3. 9 else. write: / 'all fields are different'. 10 endif. 11 12 *lines 5-9 do the same as lines 14-26* 13 14 if f1 = f2. 15 write: / f1, '=', f2. 16 else. 17 if f1 = f3. 18 write: / f1, '=', f3. 19 else. 20 if f2 = f3. 21 write: / f2, '=', f3. 22 else. 23 write: / 'all fields are different'. 24 endif. 25 endif. 26 endif.
all fields are different
all fields are different
Description:
- If f1 = f2, line 6 is true.
- If f1 = f3, line 7 is true.
- If f2 = f3, line 8 is true.
- If none of the above are true, line 9 is true.
- Lines 14 through 26 do the same as lines 6 through 10.
Using the case Statement
The case statement performs a series of comparisons.
Syntax for the case Statement
The following is the syntax for the case statement.
case v1. when v2 [ or vn ... ]. --- when v3 [ or vn ... ]. --- [ when others. --- ] endcase.
where:
- v1 or v2 can be a variable, literal, constant, or field string.
- --- represents any number of lines of code. Even zero lines are allowed.
The following points apply:
- Only statements following the first matching when are executed.
- when others matches if none of the preceding whens match.
- If when others is not coded and none of the whens match, processing continues with the first statement following endcase.
- Expressions are not allowed.
- Field strings are treated as type c variables.
Case is very similar to if/elseif. The only difference is that on each if/elseif, we can specify a complex expression. With case, we can specify only a single value to be compared, and values are always compared for equality.
An example is given as follows:
Example:
The CASE Statement Performs a Series of Comparisons
Output:1 report ztx1005. 2 parameters f1 type i default 2. 3 4 case f1. 5 when 1. write / 'f1 = 1'. 6 when 2. write / 'f1 = 2'. 7 when 3. write / 'f1 = 3'. 8 when others. write / 'f1 is not 1, 2, or 3'. 9 endcase. 10 11 * The following code is equivalent to the above case statement 12 if f1 = 1. write / 'f1 = 1'. 13 elseif f1 = 2. write / 'f1 = 2'. 14 elseif f1 = 3. write / 'f1 = 3'. 15 else. write / 'f1 is not 1, 2, or 3'. 16 endif.
f1 = 2 f1 = 2
Description:
- Line 2 defines f1 as a single character parameter having a default value 2.
- On line 4, the case statement begins and f1 is compared with 1, 2, and 3, in sequence.
- Line 6 matches and executes the write statement that immediately follows it. The remaining when statements are ignored.
- The next line executed is line 12.
Using the exit Statement
The exit statement prevents further processing from occurring.
Syntax for the exit Statement
exit.
Following is shown a sample program using exit.
Example:
sing EXIT to Stop Program Processing Dead in Its Tracks
Output: Hi Description: Exit prevents further processing, so the exit on line 3 prevents line 4 from being executed.1 report ztx1006. 2 write: / 'Hi'. 3 exit. 4 write: / 'There'.
Using the do Statement
The do statement is a basic loop mechanism.
Syntax for the do Statement
The following is the syntax for the do statement.
do [ v1 times ] [ varying f1 from s-c1 next s-c2 [ varying f2 from s2-c1 next s2-c2 ... ] ].
---
[exit.]
---
enddo.
where:
- v1 is a variable, literal, or constant.
- s is a field string having the components c1 and c2.
- f1 is a variable. The components of s must be able to be converted to the data type and length of f1.
- ... represents any number of complete varying clauses.
- --- represents any number of lines of code.
The following points apply:
- do loops can be nested an unlimited number of times.
- exit prevents further loop processing and exits immediately out of the current loop. It does not terminate the program when inside of a do loop. Processing continues at the next executable statement after the enddo.
- we can create an infinite loop by coding do without any additions. In that situation, use exit within the loop to terminate loop processing.
- Modifying the value of v1 within the loop does not affect loop processing.