Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Lensflare1699574dSo, 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. -
retoor1139474d@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
} -
cafecortado782474d@retoor Then it seems it's different from the python thingy. Nice, I like yours better.
-
retoor1139474d@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 -
Lensflare1699574d@retoor So the else case is only executed if the first evaluation of the while condition is false?
-
Lensflare1699574d@retoor yes, of course. I mean the first being false as opposed to being true, not being second :)
-
retoor1139474d@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 -
So it's a shorthand for
if (condition) {
while (condition) {...}
}
else {
}
Do you think that's a common structure that needs a shorthand? -
myss452774dI'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! -
atheist979374d@retoor do you have any concrete use case? Not something I've really thought I'd need
-
retoor1139474d@electrineer exactly. Originally you have to check cond two times as in your code. With my proposal only once
-
retoor1139474d@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
-
Lensflare1699574d@jestdotty
finally is even executed when you explicitly return out of the function (from within the try block). -
atheist979374d@retoor I'm much more likely to want the while loop to run at least once, do/while, but that's cool
-
Lensflare1699574dWhen 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.
Related Rants
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
random
while-else
feature
while
ifwhile
c
condition
python