Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8211156

While, for, do-while, try, and any block can have "else"

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Withdrawn
    • Affects Version/s: 9, 10, 11, 12, 13, 14
    • Fix Version/s: None
    • Component/s: specification
    • Labels:
      None
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      Inspired by Python. It can make Java less verbose and more powerful.

      1. while and do-while
      ----
              while(<condition>) {
                  // <body 1>
              } else {
                  // <body 2>
              }
      ----
      OR
      ----
             do {
                  // body 1
              } while (<condition>) else {
                  // body 2
              }
       ----

      This repeatedly tests the condition and, if it is true, executes the body 1; if the condition is false (which may be the first time when the condition is tested) the body 2 of the else clause, if present, is executed and the loop terminates.

      A break statement executed in the body 1 terminates the loop without executing the else clause's body 2. A continue statement executed in the body 1 skips the rest of the body 1 and goes back to testing the condition.

      @ (the updated text is taken from
      https://docs.python.org/2/reference/compound_stmts.html#the-while-statement).
      2. for
      ----
              for(<...>) {
                  // body 1
              } else {
                  // body 2
              }
      ----
      The body 1 is executed once for each item provided by the iterator. When the items are exhausted (which is immediately when the sequence is empty), the body 2 in the else clause, if present, is executed, and the loop terminates.

      A break statement executed in the body 1 terminates the loop without executing the else clause¿s body 2. A continue statement executed in the first body 1 skips the rest of the body 1 and continues with the next item, or with the else clause if there was no next item.

      @ (the updated text is from https://docs.python.org/2/reference/compound_stmts.html#the-while-statement).

      3. try
      -----
              try {
                  // body 1
              } catch (<Exception(s)>) {
                 ...
              } else {
                  // body 2
              }
      ------

      The optional else clause is executed if and when control flows off the end of the try clause*. Exceptions in the else clause are not handled by the preceding catch clauses.

      * Currently, control "flows off the end" except in the case of an exception or the execution of a return, continue, or break statement.

      @ (the updated text is from
      https://docs.python.org/2/reference/compound_stmts.html#the-while-statement) .

      A similar meaning can be for any block, e.g.:
      ----
      int m() {
      } else {
      }
      ----
      or
      ----
      static {

      } else {
      }
      ----

      In addition, the proposal could make Java more attractive to Python developers.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                abuckley Alex Buckley
                Reporter:
                aermykin Alexey Ermykin (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: