Iteration Structures

Top  Previous  Next

 

These structures allow the script logic to repeat or loop through a block of script until certain condition is met.

 

1."for" VAR ":=" EXPR "to" EXPR "do" BLOCK |
2."for" VAR ":=" EXPR "downto" EXPR "do" BLOCK |
3."while" EXPR "do" BLOCK |
4."repeat" BLOCKLIST "until" EXPR |

 

INSTR =        "for" VAR ":=" EXPR "to" EXPR "do" BLOCK |

INSTR =        "for" VAR ":=" EXPR "downto" EXPR "do" BLOCK |

 

The For statement executes a block of script for a number of predefined times by incrementing to or decrementing downto a counter for each iteration.

 

INSTR =        "while" EXPR "do" BLOCK |

 

 

The While statement executes a block of script while a certain condition is True.  When the condition becomes False, it exits the loop. In this type of iterative structure, the condition is checked before the execution of the block of script.

 

-Example :

// Read lines from a file until the end of the file is reached (not TextFileEOF=True)

While not TextFileEOF do

Begin

// Write a string into the file

TextLine:=TextFileReadln(strFile);

End;

 

INSTR =        "repeat" BLOCKLIST "until" EXPR |

 

The Repeat statement executes a block of script as long as the condition is False. When the condition becomes True, it exits the loop. In this type of iterative structure, the condition is checked after the execution of the block of script.  Therefore, unlike the While statement, the Repeat statement will always execute the block of script at least once.