Using the CASE statement to test multiple conditions
When we have more than two conditions to test, it will be beneficial to use a CASE
statement for better code readability.
How to do it...
Create a new codeunit from Object Designer.
Let's add the following global variables:
Name
Type
i
Integer
Now write the following code in the
OnRun
trigger of the codeunit:i := 2; CASE i OF 1: MESSAGE('Your number is %1.', i); 2: MESSAGE('Your number is %1.', i); ELSE MESSAGE('Your number is not 1 or 2.'); END;
It's time to save and close the codeunit.
On execution of the codeunit, you should see a window similar to the following screenshot:
How it works...
A CASE
statement compares the value given, in this case i
, to various conditions contained within that statement. Each condition other than the default ELSE
condition is followed by a colon. The same logic can be written using the IF
statement:
IF i = 1 THEN MESSAGE('Your number is %1.', i) ELSE IF i = 2 THEN MESSAGE('Your number...