Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement for-else and foreach-else in C# similar to Python's for-else and while-else?

Python's for and while loops include an optional else clause which execute if the loop exits normally (i.e. without a break statement). For example, in Python you could code:

for x in range(3):
    print(x)
else
    print("The loop exited normally")

And the output will be:

0
1
2
The loop exited normally

How would you accomplish something similar in C# in order to code something like the following:

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(x);                  
}
else
    Console.WriteLine("The loop exited normally");
like image 264
LEMUEL ADANE Avatar asked Jul 01 '11 11:07

LEMUEL ADANE


People also ask

How is foreach implemented?

forEach = function (callback) { // Do stuff... }; If you've provided one, it will loop through each item in the array, and pass in the current item in the loop, the current index, and the array itself as arguments to the callback.

Is there a for each loop in C?

The working of foreach loops is to do something for every element rather than doing something n times. There is no foreach loop in C, but both C++ and Java have support for foreach type of loop. In C++, it was introduced in C++ 11 and Java in JDK 1.5. 0 The keyword used for foreach loop is “for” in both C++ and Java.

What is difference between for and foreach loop in C#?

Difference between for loop and foreach loop:for loop executes a statement or a block of statement until the given condition is false. Whereas foreach loop executes a statement or a block of statements for each element present in the array and there is no need to define the minimum or maximum limit.

What is the syntax of foreach loop?

Syntax. For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.


3 Answers

The Python Construct for foreach-else is like this:

foreach( elem in collection )
{
    if( condition(elem) )
        break;
}
else
{
    doSomething();
} 

The else only executes if break is not called during the foreach loop

A C# equivalent might be:

bool found = false;
foreach( elem in collection )
{
    if( condition(elem) )
    {
        found = true;
        break;
    }
}
if( !found )
{
    doSomething();
}

Source: The Visual C# Developer Center

like image 56
Jon Egerton Avatar answered Sep 22 '22 02:09

Jon Egerton


Sure:

  • Use one of the fancy suggestions by the other posters

  • Use a method that performs the loop, instead of break, you can return to avoid executing code below the loop

  • Use a boolean variable you can set before you break, and test that after the loop

  • Use goto instead of break

It's a weird pattern though if you ask me. "Foreach" is always started so the word "else" does not make sense there.

like image 35
C.Evenhuis Avatar answered Sep 22 '22 02:09

C.Evenhuis


If you're referring to the for-else and while-else constructs in Python, there is a basic IEnumerable<T> extension method that simulates a foreach-else described in this article, with the following implementation:

public static void ForEachElse<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, bool> action, Action @else)
{
    foreach (var i in source)
    {
        if (!action(i))
        {
            return;
        }
    }
    @else();
}
like image 20
BoltClock Avatar answered Sep 22 '22 02:09

BoltClock