Showing posts with label Repetitive Statements. Show all posts
Showing posts with label Repetitive Statements. Show all posts

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.

REPEAT - UNTIL Loop

REPEAT - UNTIL Loop or Statement In Microsoft Dynamics NAV C/AL

Definition

REPEAT - UNTIL Loop is a repetative loop, is used for programmes having one or more statements to be executed until some condition.

Syntax OF REPEAT - UNTIL Loop

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

REPEAT Satements UNTIL Boolean Expression

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

This works in a manner like that it will work the statements until the Boolean Expression is true. The difference Between WHILE-DO loop is that the REPEAT - UNTIL Loop will execute atleast one time and then only it will check the Boolean expression true or false. But in the case of WHILE-DO loop only execute the code when a particular condition is true.

For Example:

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

REPEAT

i :=  i + 1;

Total Sales := Total Sales + Sales[i];

UNTIL Sales[i]=0;

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

The abow example works in a manner like that it will execute the statement once after that check the Boolean expression Sales[i]=0 or not if the condition is true then reapeat the statement otherwise it will exit from the loop and execute the statement immediatly after that.

WHILE .. DO Loop

WHILE .. DO Loop or Statement In Microsoft Dynamics NAV C/AL

Definition

The WHILE .. DO Loop is a repetitive statement or loop, is used in the program where a certain code is to be executed as long as some condition is true.

Syntax Of WHILE .. DO Loop

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

WHILE Boolean Expression DO Statement

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

This is simpler than the IF loop. In the case of  WHILE .. DO Loop the system check continously the boolean expression become true then the statement between BEGIN and END execute otherwise move to next statement.

For Example:

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

WHILE Sales[i+1] <> 0 DO BEGIN

     i := i + 1;

     Total Sales :=  Total Sales + Sales[i];

END;

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

This is work in a manner like that check every time the Sales[i+1] <> 0 and if the condition is true then execute the statement between BEGIN and END other wise execute after the END statement.

Repetitive Statements or Loop

Repetitive Statements or Loop

Repetitive Statements or Loops are using when we want to execute one statement or more than one statement multiple times. In the case of IF loop it will work only for one time execution only. So if the user need to execute a statement more than one times based upon some condition. 

All the repetitive statements are works in a manner like that it will work repetedly until one condition satisfy and if that condition appear then it moves the control to next programming satement after the repetitive statements. And in the case of repetitive statements always having initialize variable and increment values will appear. 

Some of the Repetitive statements are

IF Statement, WHILE .. DO, REPEAT .. UNTIL etc.

Related Posts with Thumbnails