IF Satement In Microsoft Dynamics NAV
IF Statement is the most commonly used conditional statement that work based upon the statement is TRUE or FALSE.
Conditional Statements works like first check one condition and based upon the result of this condition further flow of the program happened. Microsoft Dynamics NAV(previously Navision) language C/AL is used other conditional statements too.
If statement or IF loop is used execute a code when a purticular statement is TRUE only and some case its used to execute some statements when the statement is FALSE too.
There are mainly 2 types of IF statement specifications
- IF - THEN Syntax
- IF - THEN - ELSE Syntax
IF - THEN Syntax : -
In this type of IF statements you have to use following type of syntax
IF boolean expression THEN statement
This statement works in a manner that first the Boolean expression is evaluated and if the value is TRUE then the Statement excuted and if the Boolean expression is FALSE then the statements after the IF statement is executed. ie, the program skips the IF statements.
For example consider the following example:
======================================
IF Total > 50 THEN
MESSAGE('Total Value is grater than 50');
======================================
In this sample codes we checking that if the Total is grater than 50 or not and if the value is grater than 50 we are displaying a message that Total Value is grater than 50.
Note that the statement is indented 2 characters, this is beacuse of the better programing practice only. We can put these codes without these space also excuting the same result also we can put these statements in immediatly preceding the THEN statements.
IF - THEN - ELSE Syntax :-
In this type of IF statements you have to use following type of syntax
IF boolean expression THEN statement1 ELSE statement2
This time its works in the maner that if the boolean expression is true then execute the statement1 and if the condition is false then execute statement2.
=======================================
IF Total > 50 THEN
MESSAGE('Total Value is grater than 50')
ELSE
MESSAGE('Total Value is Less than or Equal to 50');
=======================================
In this sample codes we checking that if the Total is grater than 50 or not and if the value is grater than 50 we are displaying a message that Total Value is grater than 50. And if the value is less than or equal to that then display Total Value is Less than or Equal to 50.