loop
statement equivalent to while (true)
#6643
Replies: 9 comments 39 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
See also #2475 for conditionless |
Beta Was this translation helpful? Give feedback.
-
You can use the var myComplexCondition = true;
while (myComplexCondition)
{
…
if (…)
myComplexCondition = false;
…
} |
Beta Was this translation helpful? Give feedback.
-
Doesn't this do what you want?
|
Beta Was this translation helpful? Give feedback.
-
It would be helpful to support a simple loop directive where the loop termination (if any) is handled by logic wholly in the loop body. The reason we use contrivances like: while (true) or for (;;) Is because the language does not offer an unconditional looping structure/keyword. If adding a new keyword to C#'s grammar was trivial we could just consider adding a new "loop" or "forever" keyword, it would be that simple, done, dusted: loop
{
a = do_stuff();
if (a == b)
break;
} I've been designing an unrelated language grammar that has this, all loops are variants of "loop" with optional clauses for "while" and "until" and iterations and so on. A "loop" is effectively always infinite, unconditional, unless one includes additional clauses. I don't think a suggestion for supporting a new simple way to express unconditional loops in C# is likely to go anywhere myself. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
May I respond to your remarks about me? Read the OP, it suggests adding another way, a new way to express a non terminating loop to the language, that actually isn't trivial with C# (despite your claims to the contrary) and I think that mentioning this and the reasons for it is pertinent to the discussion, hence me saying what I said. |
Beta Was this translation helpful? Give feedback.
-
So, going back to the beginning of the thread (i.e. looping!), what could be the objection to adding forever
{
} to the C# language? It's trivial to do, that's been established, is simpler than repeat_what_I_just_did:
do_stuff();
do_more_stuff();
goto repeat_what_I_just_did; (despite several recent recommendations to leverage |
Beta Was this translation helpful? Give feedback.
-
It's pretty much the same reasoning that came up with the |
Beta Was this translation helpful? Give feedback.
-
C# currently only offers loops that are bound to specific operations,
while
which breaks once the condition isn't met,for
which provides a handy initialization and incrementation syntax sugar, andforeach
which iterates over collections.I've found myself using
while (true)
almost exclusively in certain scenarios, because most of the times the conditions to break are complex and would only be more problematic if I had to integrate it inside the condition. Many are the times that the breaking condition is in the middle of the looped body, meaning your best option is to evaluate the condition in its place and manually break the loop.Rust provides a
loop
keyword and supports a native construct forwhile (true)
and would feel more intuitive to type, as the developer is then asked to manually break the loop inside the body on demand without accounting for one special condition that's declared in a point above.Beta Was this translation helpful? Give feedback.
All reactions