could u please show me the codes too?

(example may be)

Recommended Answers

All 8 Replies

>>how to do the break?

Whats that? Some kind of new dance?

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

switch(condition)
{
    case 1: //does something
    break;
    case 2: //does something else
    break;
    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
  ++i;
};

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
  ++i;
};

@mike_2000_17- thanks! 4 d info , im gonna try this one too....

>>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)

Hi,

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

output :
01234

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).

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.