Member Avatar for Boyet728

I am very new to programming and I have an assignment, but I am not asking for the solution. I just want to understand what is being asked. Our instructor presented two codes using a break and continue statement. He stated that both methods are bad programming practices. Which I think I know the answer to that but what he wants us to do is redo the code or improve it. Here id the code

public class Break
{
      public staic void main( String args[] )
      {
            int count;

            for (count = 1; count <= 10; count++ )
            {
                   if (count == 5 )
                   break;

                   System.out.print( "%d ", count );
            }
            System.out.printf(c"\nBroke out of loop at number: %d\n", count );
}
}

How can you improve this? The only thing I can think of is the variable was declared a value inside the for loop, and if the variable changes it can create an infinite loop. So my solution was to declare the variable as a static variable to create a value that could never be changed and could not create an infinite loop. Was that the one being asked?

Recommended Answers

All 5 Replies

It's pretty easy; make full use of the conditional expression which gets tested before the body of the loop is executed. When the given condition is reached or encountered within the loop body, set a boolean flag and test the same in your loop conditional.

for(int i = 0; i < 10; ++i) {
  if(i == 5) {
    break;
  }
}

boolean done = false;
for(int i = 0; i < 10 && !done; ++i) {
  if(i == 5) {
    done = true;
  }
}

Of course you can use anything instead of a boolean but the basic premise of the solution remains the same. Make sure you skip normal execution after setting the flag otherwise the semantics of the solution changes.

Member Avatar for Boyet728

Thanks s.o.s, I wasnt sure about the question he was asking, now that you described it its clearer now. I just didnt know how to approach the problem, I searched and searched on the web and books, but just simply asking here would enlighten me. Again thanks

> He stated that both methods are bad programming practices.

No, it isn't; don't fall into such debates as anyone who tries to convince you such is just selling snake oil. If it really were that bad, you wouldn't have found Sun's internal implementation of most of the library classes to be cluttered with them.

IMO, not all things are bad in an absolute sense; there are just good and bad ways of using the same thing.

commented: Exactly what I thought when I read that. +16

Erm... I wonder if the solution your instructor wanted you to get at was to just make the loop go up to 5?

Slim chances since this seems to be a question from a programming course and not a tricky interview question. Though it is one of the logically correct solutions, if this really were the case, no break would be needed in the original snippet. Plus the breaking condition can be something which can't be computed before hand or placed in the loop conditional, hence the suggested solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.