6
retoor
81d

So, a while ago i thought i was the inventor of the while-if. If a while statement fails, it would execute the else behind it. I had that idea for the C language:

It looks like this:
while(false){
// will not be executed since while condition is false
}else{
// will be executed since while condition is false
}

I've contacted the C work group if it is something to build in C since it prolly won't break any existing code bases.

I was enthousiast. Imagine if you could invent a new feature to such a classing language.

I got response back: is it like the python while-else?

Me, been while have been python developer for a while, finds out NOW that python has it already! Damn, such a great language.

while False:
# won't be executed
else:
# will be executed

DAMMIT! Still, they said that it doesn't mean it won't become a standard and got requested more examples. Did that ofc. Let's hope

Comments
  • 3
    I think I never had a use case for that but I’m not against it. :)
  • 2
    So, the only way to get out of the loop without triggering the else case would be to break or return?

    Now that I think about it, an else case is weird if the condition is being checked multiple times, like in the while loop.
  • 2
    @Lensflare no break needed. It just works as regular loop. It executes the else i ran zero times.

    int i = 0;

    int max = 10;

    while(i < max){

    // stops after ten times

    i++;

    } else {

    // won't be executed

    }
  • 1
    @retoor Then it seems it's different from the python thingy. Nice, I like yours better.
  • 1
    @cafecortado thanks, please say that to the C work group :P

    If they would really consider it, I have to write some paper about it. No idea how to do such thing. That's for me prolly way harder than writing code. I'm not experienced in it
  • 3
    @retoor So the else case is only executed if the first evaluation of the while condition is false?
  • 4
    Too confusing.
  • 0
    @Lensflare if the first is false there will never be a second one so yes :)
  • 1
    @retoor yes, of course. I mean the first being false as opposed to being true, not being second :)
  • 1
    Make it symmetric and execute the else case in a loop as well.
  • 1
    @Lensflare Then you need a second condition. Can't have a while without one. Why would you ever want a while when you just did do a while? :P I've put a while in your while so you can go whilin' while whilin'

    @jestdotty no. That would always be executed. The code in else statement only if the while didn't run. Instead of if() you have now while(). That's how it works
  • 3
    So it's a shorthand for
    if (condition) {
    while (condition) {...}
    }
    else {
    }

    Do you think that's a common structure that needs a shorthand?
  • 3
    I've been reading this thread and posts 5x, not gonna lie. And now when I think about it, why hasn't this been introduced before?

    There's been hella lot more unecessary syntatic sugar introduced before, why this couldn't make the list?

    If there's some feature request or something similar on C workgroup, feel free to share it, I'd upvote it for sure!
  • 2
    @retoor do you have any concrete use case? Not something I've really thought I'd need
  • 0
    @electrineer exactly. Originally you have to check cond two times as in your code. With my proposal only once
  • 0
    @atheist see code of @electrineer. You can't tell me you've not written that many times. I personally use it for a lexer that reads different types of data, numbers, operators, symbols. I want to nest these loops. The change is minimal and simple. Very C worthy
  • 0
    @jestdotty
    finally is even executed when you explicitly return out of the function (from within the try block).
  • 1
    @retoor I'm much more likely to want the while loop to run at least once, do/while, but that's cool
  • 0
    @atheist Swift has this
  • 1
    When working with while loops, I find it much more readable to make it while(true) and then exit with "break" or "return" on some condition living inside the loop.
Add Comment