Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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…?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to use a quick fix and it didn’t show other options though…

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading