From the course: Learning C#

Using break and continue - C# Tutorial

From the course: Learning C#

Using break and continue

- All right. So, so far we've seen how the four and wild loop constructs give programs a way to perform a set of instructions repeatedly. And in this example, we're going to learn about two other statements that give you finer grained control over how loops execute. And these are the break and continue statements. So here in my code, in the break continue folder I'm going to open up the program file. So sometimes you want to be able to stop the execution of a loop before the ending condition is reached. So for example, suppose I had a list or an array of integer values, and I wanted to find the first instance of a value that is larger than 40. So I can have a for each loop that iterates over all the values and prints each value. So I can use the break statement, to terminate the loop early like this. So I can write if val is greater than or equal to 40 then break. So I've got my for each loop. It's going to get the value in all the values. And if the value is greater than or equal to 40 then I break. The break statement is going to stop the loop from executing any further, because after all there's no point in continuing the process when I've found the first value that I'm looking for. So let's go ahead and save this and then let's go back here and we'll bring up our integrated terminal. All right So we're going to print out the values until 40 is found or the value larger than 40 is found. So I'll run this. And you can see that when I reached the value of 41, the loop stops. So that's the break statement. The continuous statement is a little bit different. It causes the loop to skip over the rest of the statements in this iteration and continue to the next iteration of the loop. So let's close this terminal, go back to the code. All right. So let's suppose that I wanted to skip over any number in the twenties while I'm iterating over this list. So here inside my loop, what I want to do is skip. If the current number is somewhere in the twenties I want to skip it. So what I would do is write something like this. I'd write if val is greater than or equal to 20 and the val is less than or equal to 29. So in the twenties, I'm going to continue. So let's go ahead and comment out the previous example. So I'll select these lines, control slash that's command slash on the Mac, just a reminder. So let's save and let's go ahead and bring our terminal back up and let's run again. And now, as we're printing out each value, you can see that when the value is 15, 7, right then 12, then we get to 23. And since that's greater than, or equal to 20 and less than or equal to 29, the continue statement executes which means all the rest of the code that's in here which at the moment is just this right line. But all the rest of the code in the loop just gets skipped. And then the loop just goes back up to that value. And we skipped the 23, it goes straight to the 41, right? Then we skip the 28 and go right to the 9 and then 17 and 36. So these two statements give you greater control over how your loops execute their logic and can help simplify the code needed to build some more granular looping algorithms.

Contents