From the course: Learning C#

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Using break and continue

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…

Contents