UiPath While Loop Example |Video upload date:  · Duration: PT5M12S  · Language: EN

Compact guide to building and controlling a While loop in UiPath for RPA with condition examples variable updates and common pitfalls

Quick overview

If you want your UiPath automation to repeat work until something sensible happens you will love While loops and also hate them when they go infinite. This guide walks through a practical While loop setup for RPA with straight talk and useful debugging tips. You will see variables setup, Assign activity usage, VB expression examples, and ways to avoid the dreaded infinite loop that eats CPU and happiness.

Define your variables first

Start by creating clear variables. Ambiguous names are how workflows become haunted.

  • counter as Integer with starting value 0. This is your loop meter.
  • maxRetries as Integer to enforce a safety cap.
  • status as String for any state you want to log.

Add the While activity and set the loop condition

Drag a While activity into your sequence and give it a VB expression that matches the variable type and intent. For example use a condition like counter < maxRetries or status <> "Done". If the condition type is off you will get logic surprises instead of results.

Place work inside the loop

Put the actions the robot should repeat inside the While body. Common choices in UiPath include Click, Read CSV, Invoke Code and other activities. Add Write Line actions to trace progress. When interacting with the UI include short delays and robust selectors to make runs less flaky.

Example loop body checklist

  • Call the API or click the UI element
  • Parse a row or update a file
  • Log iteration with Write Line
  • Increment the counter with an Assign activity like counter = counter + 1
  • Optionally use Break when a stopping condition is met

Guardrails to avoid an infinite loop

Never assume the loop will stop by magic. Add a safety counter or a timeout check inside the loop. An infinite loop will spike CPU and freeze schedulers which is bad for automation and for your project manager.

  • Maintain counter and compare to maxRetries
  • Wrap risky actions in Try Catch so exceptions do not crash the whole run
  • Use a Break activity when an alternate stop condition is reached

Debugging and logging tips

Logging saves you from blind guessing. Use Write Line liberally to show the counter value and key variables inside the loop. If a run behaves oddly use breakpoints and step through so you can watch the VB expression evaluate in real time.

  • Log iteration number and key state
  • Test with small numbers first before scaling up
  • Monitor CPU usage when debugging potential infinite loops

When to use Do While instead

If the workflow must execute at least once pick Do While. It behaves similarly but checks the condition after the first iteration. Both are valid for UiPath automation depending on whether you need a guaranteed first pass.

Recap and quick checklist

Build While loops with clear variables use Assign activity to update counters craft correct VB expressions for the loop condition log progress and add Try Catch and a safety counter to prevent runaway automation. Follow these steps and your RPA project will be less likely to wake you up in the middle of the night.

Now go forth and automate responsibly. Your scheduler and your CPU will thank you.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.