C
C#2y ago
bkingb

❔ ✅ break 2 for cycles at the same time

My question is in the code
for (int i = 0; i < heights.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (heights[i] < heights[j])
{
Console.WriteLine("Van olyan ember, aki alacsonyabb, mint a mögötte állók valamelyike!");
break; // I want to break all the 2 for cycles here what do I do?

}
}
}
for (int i = 0; i < heights.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (heights[i] < heights[j])
{
Console.WriteLine("Van olyan ember, aki alacsonyabb, mint a mögötte állók valamelyike!");
break; // I want to break all the 2 for cycles here what do I do?

}
}
}
6 Replies
Anton
Anton2y ago
you have two choices: 1. put a label and use a goto https://stackoverflow.com/a/1548160/9731532 2. make a local function where you can use normal returns to implement that
Stack Overflow
is there a equivalent of Java's labelled break in C# or a workaround
I am converting some Java code to C# and have found a few labelled "break" statements (e.g.) label1: while (somethingA) { ... while (somethingB) { if (condition) { ...
Anton
Anton2y ago
the second option is better
bkingb
bkingb2y ago
Thanks
Anton
Anton2y ago
np
bkingb
bkingb2y ago
!close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.