Conditional Statements in X++

1. Conditional Statements 

Conditional statements in programming define conditions under which certain functions are performed. Conditional statements use logical expressions that are evaluated and return a value of either true or false. There are three primary conditional statements: 


• If statement 

• Switch statement 

• Ternary operators All these statements are evaluated using operators. 


   1.1  If 


    The if statement is the simplest control statement. It checks whether a condition is true or false. If the condition is satisfied, all the code within the braces '{}' is executed. The syntax for an if statement is as follows:


    The following is an example of an if statement. If the variable a is greater than 10, the value of a will be printed on the screen. 



    The following is an example of an if statement using multiple expressions to evaluate the condition. 


    1.2 If…else


    An if statement checks for only one possibility and ignores all other conditions. An if…else statement checks one condition and if true, the block of code is executed. Otherwise, an alternative statement is executed. The syntax for an if…else statement is as follows:



    The previous conditional formulas allow for only two alternative outcomes. A program might have to check more than two alternatives. To check for multiple alternatives, you can use an if…else...if statement. The syntax for this statement is as follows: 




    1.3 Ternary Operator 


    This conditional statement behaves exactly like an if…else statement. The main reason to use the ternary operator is convenience in coding. Its syntax is as follows: 



    The condition is checked first and if true, statement 1 is executed, if false, statement 2 is executed. However, the two expressions following the question mark (?) must be of the same data type. 

This example using the ternary operator is equivalent in logic and results to the previous example using the if...else statement. 


    1.4 Switch 


    A switch statement acts as a multi-branch control statement that defines an expression and whose result leads to a specific program execution. The switch statement considers the result and executes code, depending on possible outcomes of the expression. These are known as cases. Each of these cases is listed in the body of the statement. 


    Following the colon after each case are statements that execute if the expression satisfies the case. There can be any number of statements following a case in a switch statement. The body of a switch statement is enclosed in braces '{}'. The following shows the syntax for a switch statement: 


    Example:


2. Loops 


Repetitive statements, also known as loops, conditionally control data input and output. There are three main loops in X++: 


• While loop 

• Do while loop 

• For statement 


    2.1 While 


    The while loop evaluates a condition and executes statements, depending on whether the condition is satisfied. The loop continues to check the condition, and as long as the condition is true, it continues to execute the statements. As soon as the condition becomes false, the statement is exited. The syntax is as follows: 


    This is a simple while statement that counts from 1 to 5




    HINT: The previous example uses the counter++which can increment any integer by 1. You can also set the increment to any value by using the variable+=<# to increment>syntax. For example: counter+=2; //This increments the counter variable by two every time Notice the condition is evaluated before the statements are executed. In the previous example, it means that if the counter variable is greater than five when it reaches this loop, it does not execute. Therefore, a while statement can be executed zero or more times. 


    2.2 Do..while 


    The function of a do-while statement is almost identical to the while statement. The main difference is that the condition is evaluated after the statement executes. The effect is that the loop always runs at least one time. The following is the syntax for a do-while statement:


    2.3 For 


    The for statement uses an incrementing counter variable, and a condition, to determine how long it will continue to loop. The parameters in a for statement include three elements: 

• The initial value of the variable. 

• The condition under which the loop will continue. 

• The amount the variable is increased or decreased by. 

• The syntax can be defined as follows:


    For loops are frequently used when navigating through an array. The following is an example of how to use a for loop to print each element of a string array called 'ABC' that contains 10 strings: 

                    


A for loop and a while loop performs the same function. However, the for loop is more condensed in structure. In a while loop, if the counter variable is not incremented, an infinite loop can occur. This is not possible with a for loop because you receive a syntax error if that part of the condition is not qualified. 





Comments

Popular posts from this blog

How to customize electronic reporting in D365

Batch parallelism or multithreading in Dynamics 365 for Finance and Operations

How to Enable/Disable a Form Button with X++

How to create and run the Runbase batch class in D365

Build HTML and send email in D365 FO with X++

Difference between InMemory and TempDB tables in D365 F&O

How to apply a package in LCS Dynamics 365 F&O

Customize the standard excel template in D365FO

How to write the event handler in D365