Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

7.14 While and Do Loops

While and Do Loops

The While and Do loops all have the standard syntax. The While keyword is used to continue looping as long as the condition evaluates to True. The While block is terminated with a Wend statement. The Do loops are terminated with a Loop statement. The Until keyword is used to continue looping when a condition evaluates to False. You can exit a Do loop with an Exit Do statement.

Crystal syntax uses parentheses to define the code block.

Code template for While … Do:

While true_condition Do
(
…code…
)

Code template for Do..While:

Do
(
…code…
) While true_condition

The next example takes an array of names (populated elsewhere in the program) and uses a While loop to combine the names into a single string.

//Combine all names from an array into a single string
Global StringVar Names;
StringVar NameList;
NumberVar Counter := 1;
While Names[Counter] <> “” Do
(
NameList := NameList & Names[Counter] & ” “;
Counter := Counter + 1;
);
//Return the name list
NameList;