Do you mean Case Break statements where it's like:

    case 1: //does something
    case 2: //does something else
    default: //does the default action if now condition is met

Is that what you were looking for?

@first person@- haha!! your funny huh!

@spartan-S63@- thanks! this is what i exactly looking for.!! thanks again!

The other use of break is to "break" out of a loop. For example, the following three loops have essentially the same behavior (of course the first one is the simplest, best and most efficient):

const int N = 20; //upper-bound

for(int i=0; i<N; ++i) {
  //.. do something

int i = 0;
while(i<N) {
  //.. do something

int i = 0;
while(true) {
  if(i == N)
    break; //this will terminate the loop and jump to right after the "};" of the loop
  //.. do something

>>of course the first one is the simplest, best and most efficient
"Simplest and best" are subjective terms. And about it being efficient, not completely sure about that. Usually the compiler might make them the same instruction with optimization.

>>the following three loops have essentially the same behavior
Just thought I would point this out, in the while loop, long as "//do something" does
not contain a continue or a break statement, its essentially the same, or else there could be a infinite loop or other bugs.

could u please show me the codes too?

(example may be)


for(int i=0; i<10; i++)

output :

Break is used to break the iterations.

@FirstPerson: Let me clarify:
>>"Simplest and best" are subjective terms
You're right they are subjective, so let me expand on my subjectivity. I say "simplest" because it is most concise and all the necessary information about start, condition and increment of the loop are in one line that is easy to read for the mildly-trained eye. I say "best" because the integer value i is local in scope to the loop only, while the others will keep it alive until the next end-of-scope. But you are right, the compiled code will most probably be the same and as efficient in either cases (except perhaps for the additional variable in the scope containing the loop, which some may argue could be a tiny bit more efficient).

This article has been dead for over six months. Start a new discussion instead.