I saw a lot of examples with "return" which I use too but sometimes I want to avoid creating new functions and use a loop instead:
while (1) {
if (FileExists(file)) {
contents = OpenFile(file);
if (SomeTest(contents)) {
DoSomething(contents);
break;
}
}
DefaultAction();
break;
}
while (1) {
if (FileExists(file)) {
contents = OpenFile(file);
if (SomeTest(contents)) {
DoSomething(contents);
break;
}
}
DefaultAction();
break;
}
If you want to write less lines or you hate infinite loops as me, you can change the loop type to "do ... while(0)" and avoid the last "break".