FOR Loop

FOR Loop or FOR Statement In Microsoft Dynamics NAV

Definition

FOR loop is a repetitive statement or loop, is used when we need to execute the code a predetermined number of times. 

FOR Loop in Microsoft Dynamics NAV, C/AL having mainly 2 types

  1. FOR .. TO Loop
  2. FOR .. DOWNTO Loop

FOR .. TO Loop

In the case of FOR .. TO Loop the value checking is incresed format and when the value reach the condition wrong then exit loop.

Syntax of FOR .. TO Loop:

--------------------------------------------------------------

FOR Control Variable :=  Start Value TO End Value

DO Statement

--------------------------------------------------------------

Here Control Variable must be a variable type of Boolean, Date, time or any numeric type. The Start value and End Value must be either expressions of same data type.

For Example:

===========================================

FOR i :=1 TO 5 DO

    Result := Result + 5;

===========================================

In this programming codes works in a manner that first intialize the value i to 1 and then check the condition that whether the i less than 5, i="i+1"

FOR .. DOWNTO Loop

In the case of FOR .. DOWNTO Loop the value checking is decresed format and when the value reach the condition wrong then exit loop.

Syntax of FOR .. DOWNTO Loop:

--------------------------------------------------------------

FOR Control Variable :=  Start Value DOWNTO End Value

DO Statement

--------------------------------------------------------------

The Control Variable, Start Value and End Value are same as  FOR .. TO Loop. Only difference is that in the case of  FOR .. TO Loop Control Variable is in the Increase format but in the case of  FOR .. DOWNTO Loop decrese format.

For Example:

===========================================

FOR i :=5 DOWNTO 1 DO

    Result := Result + 5;

===========================================

In this case programming code work in the manner like first assign the value 5 to i and then check the value 5>1 and then do the statements following. Next time i value decrese by 1 and then check the condition and soon.

Related Posts with Thumbnails