'Why does a for-yet loop not exist?
I have learnt 4 kinds of iteration statements:
- for (also called fori or forj)
- while
- do-while
- foreach
Why pray, then does a for-still loop, not exist?
What are the use cases you ask?
- Same question can be asked for a do-while loop, where we can just substitute it with an appropriate if statement surrounded by a while loop.
- One use case I can think of is the following:
(Just pseudo-code, 'cause well, this paradigm is language independent.
var stringToTokenize = 123,456,abc // expected output: string[] = { "123", "456", "abc"}
var token = "";
var[] tokens // (or string[], whatever)
// stringToTokenize will be referred to as str henceforth ('cause I am a lazy typist)
for(i = 0; i != str.length(); i++) {
// token is complete, add to the array or list
if (str[i] == ',') {
tokens.add(token);
//reset token
token = "";
continue;
}
token += str[i];
}
// one last time
tokens.add(token);
with a for-still loop this will become
for(i = 0; i != str.length(); i++) {
// token is not complete
if (str[i] != ',') {
token += str[i];
continue;
}
yet { // my made-up syntax
// will run one last time
tokens.add(token);
//reset token
token = "";
}
}
Is there something inherently wrong with this kind of construct of was it just thought of and dismissed?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|