Home > database >  Java 8, While Loop: Reduce the total number of break and continue statements in this loop to use at
Java 8, While Loop: Reduce the total number of break and continue statements in this loop to use at

Time:10-26

I've got a class that contains the following:

while (true) {
            // if a minimum element in the queue is greater than the required sweetness
            // then we are done
            if (queue.peek() >= minSweetness) {
                solutionPossible = true;
                break;
            } else {
                // if there are more than or equal to 2 elements,
                // then only solution is possible
                // because we have already checked queue.peek() for the single element
                // present, and that is less than minSweetness
                if (queue.size() >= 2) {
                    // remove minimum and 2nd minimum values
                    int a1 = queue.poll();
                    int a2 = queue.poll();

                    // again push the value to the queue
                    // after calculating the combined sweetness
                    queue.offer(a1   2 * a2);
                } else {
                    // for a single element that is less than the required sweetness
                    // no solution is possible
                    solutionPossible = false;
                    break;
                }
                // increase the total number of operations
                operations  ;
            }
        }

Here is a screenshot: enter image description here

vscode tells me to reduce the total number of break and continue statements in this loop to use at most one, so I am not as experienced as to what other method I can think of, I tried to use quick fix and It didn't work, anybody has any idea how to write this class differently...?

I tried to use a quick fix and it didn't show other options though...

CodePudding user response:

Instead of coding the while as an infinite loop, and breaking out of it, use a variable to control loop continuation or exit. In this case, I chose a boolean:

boolean decided = false;
while ( ! decided ) {
            // if a minimum element in the queue is 
            // greater than the required sweetness
            // then we are done
            if (queue.peek() >= minSweetness) {
                solutionPossible = true;
                decided = true;
            } else {
                // if there are more than or equal to 2 elements,
                // then only solution is possible
                // because we have already checked 
                // queue.peek() for the single element
                // present, and that is less than minSweetness
                if (queue.size() >= 2) {
                    // remove minimum and 2nd minimum values
                    int a1 = queue.poll();
                    int a2 = queue.poll();

                    // again push the value to the queue
                    // after calculating the combined sweetness
                    queue.offer(a1   2 * a2);
                } else {
                    // for a single element that is 
                    // less than the required sweetness
                    // no solution is possible
                    solutionPossible = false;
                    decided = true;                       
                }
                // increase the total number of operations
                operations  ;
            }
        }

By the way, I like retaining the first break. It's a style preference:

boolean decided = false;
while ( ! decided ) {
            // if a minimum element in the queue is 
            // greater than the required sweetness
            // then we are done
            if (queue.peek() >= minSweetness) {
                solutionPossible = true;
                decided = true;
                break;

            
                // if there are more than or equal to 2 elements,
                // then only solution is possible
                // because we have already checked 
                // queue.peek() for the single element
                // present, and that is less than minSweetness
                if (queue.size() >= 2) {
                ...

Retaining the first break allows elimination of the first else, reducing the nesting level by one for the code that follows.

  • Related