-6

Is break statement in Java indispensable, or even the whole "break" concept? A task can be achieved by having a break in a branch. How about if break is never introduced to the programming language? Can we achieve the same task?

Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35
  • What's wrong with my question? – Ka Wa Yip May 30 '15 at 10:59
  • We usually do not like questions that seem to be copied word by word from your assignments. You can ask such a question, but I would recommend being honest and say that it's an assignment and - even more important - add your ideas and where you are stuck. – cel May 30 '15 at 11:19
  • simply answering: Yes we can achieve same task without break, it just would require more boolean flags to get same control in program. If you remove break u also should remove continue from programming language, but think of how messy code would look with tons of flags inside. – itwasntme May 30 '15 at 11:32
  • This is not an assignment question. I am not a computer science student. I learnt programming myself. Just curious about `break` statement. – Ka Wa Yip May 30 '15 at 11:55

3 Answers3

1

I don't recommend you to use break statement if it's not strict necesary it's use.

It isn't a good practise in programming (look at this: Is it a bad practice to use break in a for loop?) and I recommend you to use another alternatives, instead of break that could make the same work.

For example, you can wrap your code into a loop that finish with the same condition in which you wanted to put the break statement.

I always try not to use this break statement but, for example when I have to use a switch I always use it. It depends of the mind of each person, some persons think that break statement it's necesary and another people, like me, try to avoid it.

I expect it helps you to see the concept more clearly.

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
0

Everything you can do with break in Java can be accomplished using state variables instead (even the break on switch statements). So in that sense, it's dispensible.

However, the resulting state variable mess is why we have break.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

All programming can be done using only if statements and recursion.

Raedwald
  • 46,613
  • 43
  • 151
  • 237