break and continue in C#
In this post, you will learn how to work with break and continue so that you can use that same knowledge and thus evolve.
- break makes the life of the cycle come to an end, that is, makes it not iterate through anything else;
- continue ignores the entire content of the loop that is following from itself and automatically moves to the next iteration.
using System;
using System.Collections.Generic;
namespace Base {
class BreakAndContinue {
public void Run() {
int counter = 0;
List<string> animals = new List<string>() {
"Dog",
"Cat",
"Chicken",
"Rabbit",
"Lion"
};
foreach (string animal in animals) {
Console.WriteLine($"Animal: {animal}");
if (animal == "Chicken")
break;
}
while (counter < 10) {
counter++;
if (counter == 5)
continue;
Console.WriteLine($"Counter: {counter}");
}
}
}
}
/*
Animal: Dog
Animal: Cat
Animal: Chicken
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Counter: 10
*/
Don’t forget to watch the video and you can always read this post in Portuguese.