I propose the addition of optional ELSE clauses to all FOR and WHILE loops in Java and C++. This ELSE clause functions in much the same way that a normal ELSE clause functions after an IF statement except that there is a loop in place of the IF statement. The ELSE clause gets executed if the loop runs to completion. To prevent to loop from completing, the programmer must use the BREAK command, which already exists in Java and C++. Currently, the BREAK command prematurely terminates the innermost loop, and causes execution to resume outside of the loop. When combined with the ELSE clause, the BREAK command will act almost exactly the same: execution will resume outside of the loop, but will also skip the ELSE clause associated with the loop.

   This feature only improves to the languages; it does not impair their existing functionality in any way. It should not be too difficult to implement as ELSE clauses already exist in these languages, as does the BREAK command. Compiling, likewise, should be straightforward, although there may be some difficulty with error detection. Since the ELSE clause gets executed after the loop runs to completion, infinite loops should not have ELSE clauses because they will never complete. It would be syntactically legal to include an ELSE clause after any loop, but the compiler will not be able to detect which clauses will go unused because it cannot detect all infinite loops. The programmer will have to be aware of this fact when coding.

   This feature will be backwards compatible in both C++ and Java. ELSE clauses on loops would not be required, so older programs without them should have no trouble compiling. Still, programmers used to only seeing ELSE clauses after IF statements may have some trouble understanding what they do and when they get executed. Programmers familiar with Python will find this feature straight forward as it already exists in that language.

   Here are two examples of source code. The left side is without the feature, and the right is with it.

bool found = false;
for(int i=0; i<howMany; i++)
{
  if(array[i] == "foo")
  {
    found = true;
    break;
  }
}

if(found)
  cout << "Located foo" << endl;
else
  cout << "foo not found" << endl;

for(int i=0; i<howMany; i++)
{
  if(array[i] == "foo")
  {
    cout << "Located foo" << endl;
    break;
  }
}
else
{
  cout << "foo not found" << endl;
}

   While there is only a slight improvement in length (9 lines vs. 6), there are other advantages. The code on the right does not have the extra variable "found", and the entire search is contained inside a single compound loop as opposed to a loop and an IF statement. Still, the code on the left may be slightly easier to read since there is a bit more separation between the code that searches and the code that decides if there was a match. This feature is not a critical one, as straightforward workarounds exist, but it can still make coding simpler in structure and require fewer variables.

   Searching through arrays is not the only applications where this feature would be useful. It can be useful whenever the programmer needs to do something a finite number of times, stop for some special cases, and be able to tell at the end if any of those special cases happened. In general, the ELSE clause after a loop is only useful if the loop utilizes a BREAK command. The BREAK command makes it easy to stop a loop once it reaches a certain point, but the ELSE clause makes it easy to detect if that actually happened.