UiPath Break and Continue Example


  • Introduction

    In this tutorial, I’d like to show you how the UiPath continue, and break activities work. In a previous tutorial, I created a little application that went through the first 11 Fibonacci numbers. You can see I’ve got a variable called Fibonacci numbers here, it’s got 11 different elements in the array, and I’ve got a for loop that loops through each of them.

    Iterating through numbers to check if it's Odd or Even

    This for-loop just prints out the value of each number through a log message. The number is item.toString , but let’s imagine that we only wanted to print out the odd numbers in Fibonacci sequence. How would we do that? Well, what we could do it is by using the mod operator and figuring out which index is in the array are odd and then, when we do encounter one, don’t print out the log message, but instead go back to the start of the loop and work on the next element. In order to do that, we would use a continue element, a continue activity in our application. So let me show you how this would be done. I’m going to add an if activity here. So, I’ll drag the, if activity into the body of the loop, and I’m going to say, you know, if the current Fibonacci number is even, don’t print it out, skip it, just go back to the start of the loop and find the next number in the array and so the condition is basically to see if the number, the item is even, or odd. Specifically, whether it’s even. So, notice that, uh, the, for each here, the argument type is set to Object. That’s the default, of course the array actually has nothing but int’s in it. So here I’m going to actually specify that int32 is the argument type for the, for each loop. But something interesting to note, they’ll probably ask you a question too, about what the default variable is for a, for each loop on the UiPath Associate Exam. So be, be ready, be careful about that but now that we’ve said that this is actually going to be an int32 value. What I want to do is I want to check to see if that item mod 2 equals 0 because if that number divided by 2 has no remainder, that means it’s an odd number.

    Print out odd numbers

    And I said, we only want to print out. That means it’s an even number and we only want to print out the odd numbers. So how do we do that? Well, if the number is even, what we want to do is just do a continue, so I’ll type in continue, drag the continue activity over and now if I run this sequence in the output window, we should not see the number 2, 8 or 34. So let me click Ctrl+S, run the file. First, give me a message box telling me that there’s 11 elements in the array. But when I look at the output, notice, 1, 1, 3, 5 21 35, 89. We didn’t print out any of those even numbers. Okay. So that’s the idea of the continue. If you hit a continue, it says don’t break out of the loop. Don’t stop the loop but stop any further execution of logic during this iteration and go back to the start of the loop activity, the iterative component, and then start again.

    Using Break Activity

    Now let’s say we didn’t want to print out any numbers greater than 9, Right. We don’t want any, whole numbers. So, in order to do that, what you can do is you can add a break and a break just breaks you out of the activity altogether. So, I’m going to add another if statement- here, I’m not going to do a nested-if statement, I’m going to add another if statement entirely, and I’m going to say if the item is greater than 9. So, if the number is greater than 9, then I’m just going to break out of the loop.Okay and so, this means as soon as we hit something that’s greater than 9, we’re just going to stop execution entirely. We’re not going to go to the top of loop and run another time. We’re just going to exit execution completely and go to the, the end of the loop and in fact, I might even add a _message box activity right at the end of the loop, right at the end of the sequence that says we are done with the loop. Okay. And that’ll just prove that yeah. We’ve actually exited out of the loop completely. So, I’m going to clear the output window. I’m going to run the file. And when I click run, well, we’re going to get a message box saying that there’s 11 elements. Then the message box will come up and say, we are done the loop.
    Now, in the meantime, before it said we are done with the loop. If we actually went back to UiPath, we would notice that we actually got some output before that message box popped up and it printed out all of the odd Fibonacci numbers that were less than 10. Once it got to a number of greater than 10, it just broke out of the loop and said, we are done with the loop and there you go. That is the idea behind working with UiPath, continue and UiPath break statements.

UiPath Continue and Break Tutorial

UiPath Break and Continue Example
This UiPath break and continue example shows how to control iterative logic in RPA applications.

Test Your Knowledge

  1. Which activity is used to skip the rest of the code inside a loop, for the current iteration only?
    • Break activity
    • Continue activity
  2. Which of the following activity is not used to alter the flow of a iteration loop?
    • Break activity
    • Continue activity
    • For each activity
  3. Which of the following activity should be used to terminate the program flow from the current loop?
    • Break activity
    • Continue activity
    • For each activity

References that you might want to have a look at :

Where to find out more about the UiPath Continue and Break activities?

If you’d like to go beyond this UiPath Break and Continue Example, visit the UiPath Forum.

Discuss with Us on Twitter
Get UiPath Certified