To break out of a loop prematurely (before the exit condition is met), you can use one of the following keywords:
□ break
□ return
□ throw
□ goto
The break
keyword allows you to break out of a loop prematurely:
int counter = 0;
do {
Console.WriteLine(counter++);
} while (true);
In this example, you increment the value of counter
in an infinite do-while
loop. To break out of the loop, you use a if
statement to check the value of counter. If the value exceeds 100, you use the break
keyword to exit the do-while
loop.
You can also use the break keyword in while
, for
, and foreach
loops.
The return
keyword allows you to terminate the execution of a method and return control to the calling method. When you use it within a loop, it will also exit from the loop. In the following example, the FindWord()
function searches for a specified word ('car') inside a given array. As soon as a match is found, it exits from the loop and returns control to the calling method:
class Program {
static string FindWord(string[] arr, string word) {
foreach (string w in arr) {
//--- if word is found, exit the loop and return back to the
// calling function---
if (w.StartsWith(word)) return w;
}
return string.Empty;
}
static void Main(string[] args) {
string[] words = {
'-online', '4u', 'adipex', 'advicer', 'baccarrat', 'blackjack',
'bllogspot', 'booker', 'byob', 'car-rental-e-site',
'car-rentals-e-site', 'carisoprodol', 'casino', 'casinos',
'chatroom', 'cialis', 'coolcoolhu', 'coolhu',
'credit-card-debt', 'credit-report-4u'
};
Console.WriteLine(FindWord(words, 'car')); //---car-rental-e-site---
}
}
The throw
keyword is usually used with the try-catch-finally
statements to throw an exception. However, you can also use it to exit a loop prematurely. Consider the following block of code that contains the Sums()
function to perform some addition and division on an array:
class Program {
static double Sums(int[] nums, int num) {
double sum = 0;
foreach (double n in nums) {
sum += num / n;
}
return sum;
}
static void Main(string[] args) {
try {
Console.WriteLine(Sums(nums, 2));
}
}
When the foreach
loop reaches the fifth element of the array (0), it throws an exception and exits the loop. The exception is then caught by the try-catch
loop in the Main()
method.
The goto keyword transfers program control directly to a labeled statement. Using goto is not considered a best practice because it makes your program hard to read. Still, you want to be aware of what it does, so the following example shows its use:
string[] words = {
'-online', '4u', 'adipex', 'advicer', 'baccarrat', 'blackjack',
'bllogspot', 'booker', 'byob', 'car-rental-e-site',
'car-rentals-e-site', 'carisoprodol', 'casino', 'casinos',
'chatroom', 'cialis', 'coolcoolhu', 'coolhu',
'credit-card-debt', 'credit-report-4u'
};
foreach (string word in words) {
if (word == 'casino')
goto Found;
}
In this example, if the word casino is found in the words
array, control is transferred to the label named Found:
and execution is continued from there. If the word is not found, control is transferred to the label named Resume:
.
Skipping an Iteration
To skip to the next iteration in the loop, you can use the continue
keyword. Consider the