disclaimer: I'm a university student who's one-year-new to programming. Please don't slaughter me in your responses as I am still a human being
I have an idea that I want some feedback on. I am currently learning C and we just covered while and do loops. The following is some pseudo-code and an interpretation from the perspective of a compiler in a theoretical sense:
the compiler reads the following lines:
while(conditions){
body
}
and sees that it is a simple while statement and executes it as such. Then, the compiler reads the following lines:
do{
body
}while(conditions);
and sees that it is a simple do / while statement since it terminates with a semicolon and executes it as such. Then the compiler reads the following lines:
do{
body1
}while(conditions){
body2
}
and this is the definition of my idea, a do + while statement. The compiler knows this is a do + while statement since instead of terminating with a semicolon, the while statement opens a new body using a curly brace and then closes with a curly brace at the end of body2.
In my mind this unifies do and while into a modular feature set where you can either use while, do, or do + while. Functionally, the program would execute body1 at least once because it is not bounded by a condition set, then it would check the conditions of the while loop. If the conditions are not satisfied, the do + while loop would terminate. If the conditions are satisfied, the program would execute body2. After body2 is executed, the program would return to the top of the loop, which is body1, and execute it again (since it is unconditional), and then proceed down to the while conditions, looping over and over until the conditions are not satisfied. From what I can gather, it essentially adds nothing new in terms of base architecture. It simply takes a pre-conditional body1 from do and couples it to a post-conditional body2 from while and loops them together.
This (what I believe to be elegant) solution potentially bridges the gap between having separate functions dedicated to evaluating while conditionals so that you're not clogging up the conditions portion of the while loop with long lines of code, but also can clean up code that is on the cusp of needing its own condition function. Like for example you've got one condition that needs an if statement or something else that requires just a few lines of code. Rather than create an entirely new function when the other conditionals are so simple, you can execute a few lines of pre-conditional code every loop to check the validity of one condition before passing it to while(conditions) and have it be clean and readable.
Is there already an elegant solution to this problem? Would any of you actually use an implementation that looks like this? I look forward to hearing feedback on this idea. Even if it is negative I feel like it was a good thought experiment and I hope some of you feel the same way at least.