/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/statements/snippets/jump-statements/BreakStatement.cs
90 строк
2 KB
Bill Wagner
Use Collection expressions for all language reference samples (#37993)
16 ноя 2023, 23:06
Не верифицирован
16 ноя 2023, 23:06
ace7aec
Код
Авторство
О чём код?
namespace JumpStatements; public static class BreakStatement { public static void Examples() { BasicExample(); NestedLoop(); SwitchInsideLoop(); } private static void BasicExample() { // <BasicExample> int[] numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; foreach (int number in numbers) { if (number == 3) { break; } Console.Write($"{number} "); } Console.WriteLine(); Console.WriteLine("End of the example."); // Output: // 0 1 2 // End of the example. // </BasicExample> } private static void NestedLoop() { // <NestedLoop> for (int outer = 0; outer < 5; outer++) { for (int inner = 0; inner < 5; inner++) { if (inner > outer) { break; } Console.Write($"{inner} "); } Console.WriteLine(); } // Output: // 0 // 0 1 // 0 1 2 // 0 1 2 3 // 0 1 2 3 4 // </NestedLoop> } private static void SwitchInsideLoop() { // <SwitchInsideLoop> double[] measurements = [-4, 5, 30, double.NaN]; foreach (double measurement in measurements) { switch (measurement) { case < 0.0: Console.WriteLine($"Measured value is {measurement}; too low."); break; case > 15.0: Console.WriteLine($"Measured value is {measurement}; too high."); break; case double.NaN: Console.WriteLine("Failed measurement."); break; default: Console.WriteLine($"Measured value is {measurement}."); break; } } // Output: // Measured value is -4; too low. // Measured value is 5. // Measured value is 30; too high. // Failed measurement. // </SwitchInsideLoop> } }