1. Activity Program Name
System.Activities.Statements.DoWhile
2. Description
Studio Version ~2.0.2.0
Quick Access: Toolbox > Control Flow > Do While
Studio Version 2.0.3.0~
Quick Access: Toolbox > System > Control Flow > Do While
This activity repeats a specified process as long as a condition is met. Since the condition is evaluated after the specified process is executed, the process will be executed at least once, regardless of the condition specified.
3. Properties
4. How to Use
Let’s create a robot that uses the "Do While" activity to display a random number between 1 and 100 in a message box until an odd number is shown.
First, place a message box to indicate the start of the process. Drag and drop the Core > Message Box activity from the toolbox to the design panel.
In the "Text" field of the activity, specify a message such as: "A random number between 1 and 100 will be displayed until an odd number appears." (You can also set this through the Input > Text property in the property panel).
Next, drag and drop the Control Flow > Do While activity from the toolbox to the design panel.
In the "Body," describe the process you want to repeat. In this example, it is the process to display a random number between 1 and 100 in a message box. In the "Condition," specify the condition for repetition. If the specified condition is True, the process in the Body will repeat. In this example, the condition is "if the number is even." Since the Condition is placed after the Body, the process will be executed at least once, regardless of the condition.
Inside the Body, add the process that you want to repeat. Drag and drop the Control Flow > Sequence activity from the toolbox into the Body section of the design panel.
Now, let's add the process of generating a random number between 1 and 100 to the Sequence. Drag and drop the Original > Assign activity from the toolbox into the Body section of the design panel.
In the left-hand side ("To") of the assign activity, specify the variable name where the generated random number will be stored. (You can also set this via the Other > To property in the property panel). If the variable does not already exist, create it. The variable type should be Int32.
You can use the Random().Next() function with specified lower and upper bounds to generate a random integer within that range. In the right-hand side ("Value") of the assign activity, input new Random().Next(1, 100). (You can also set this via the Other > Value property in the property panel).
Next, let's display the generated number. Drag and drop the Core > Message Box activity from the toolbox into the Body section of the design panel, and set the "Text" field of the activity to randomNumber.ToString. (You can also set this via the Input > Text property in the property panel).
Now, let’s specify the condition for repeating the process in the Body. In this example, we want the process to repeat if the number is even. Therefore, the condition is "when the remainder of the number divided by 2 is 0." In the Condition field, set randomNumber Mod 2 = 0. The Mod operator is a modulo (remainder) operator that returns the remainder of division.
The setup for the Do While activity is now complete. After exiting the loop, let’s display a message indicating the end of the process. Drag and drop the Core > Message Box activity from the toolbox into the design panel, and set the "Text" field of the activity to randomNumber.ToString + " is an odd number, so the loop has ended." (You can also set this via the Input > Text property in the property panel).
Now, click Home > Start or press the F5 key to run the process.
Execution Results
The message box will display a random number until an odd number is shown. Since the integer is generated randomly, the number of repetitions and the integers displayed will differ each time you run the workflow.
● Start message:
"A random number between 1 and 100 will be displayed until an odd number appears."
● Repeating process within the Body:
The message box displays random numbers until an odd number is found.
● End message:
The final message states the odd number and indicates the process is finished.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article