IF-THEN-ELSE statement

IF is the basic conditional statement of most programming languages. It operates in C/AL much the same as how it works in other languages. The basic structure is: IF a conditional expression is True, THEN execute Statement-1ELSE (if condition not True) execute Statement-2. The ELSE portion is optional. The syntax is as follows:

IF <Condition> THEN <Statement-1> [ ELSE <Statement-2> ]

Note that the statements within an IF do not have terminating semicolons, unless they are contained in a BEGIN - END framework. IF statements can be nested so that conditionals are dependent on the evaluation of other conditionals. Obviously, one needs to be careful with such constructs, because it is easy to end up with convoluted code structures that are difficult to debug and difficult for the developers following us to understand. In the next chapter, we will review the CASE statement that can make some complicated conditionals much easier to format and understand.

As we work with NAV C/AL code, we will see that often <Condition> is really an expression built around a standard C/AL function. This approach is frequently used when the standard syntax for the function is "Boolean value, function expression". Some examples follow:

  • IF Customer.FIND('-') THEN... ELSE...
  • IF CONFIRM('OK to update?',TRUE) THEN... ELSE...
  • IF TempData.INSERT THEN... ELSE...
  • IF Customer.CALCFIELDS(Balance,Balance(LCY)) THEN...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.41.229