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
-
Lensflare17468115dSo, 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. -
typosaurus12155115d@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
} -
cafecortado7829115d@retoor Then it seems it's different from the python thingy. Nice, I like yours better.
-
typosaurus12155115d@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 -
Lensflare17468115d@retoor So the else case is only executed if the first evaluation of the while condition is false?
-
Lensflare17468115d@retoor yes, of course. I mean the first being false as opposed to being true, not being second :)
-
typosaurus12155115d@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 -
electrineer29794115dSo it's a shorthand for
if (condition) {
while (condition) {...}
}
else {
}
Do you think that's a common structure that needs a shorthand? -
myss4450115dI'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! -
atheist9981115d@retoor do you have any concrete use case? Not something I've really thought I'd need
-
typosaurus12155115d@electrineer exactly. Originally you have to check cond two times as in your code. With my proposal only once
-
typosaurus12155115d@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
-
Lensflare17468115d@jestdotty
finally is even executed when you explicitly return out of the function (from within the try block). -
atheist9981115d@retoor I'm much more likely to want the while loop to run at least once, do/while, but that's cool
-
Lensflare17468115dWhen 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