473,888 Members | 1,324 Online

# Not clear on the use of break statements

19 New Member
Hi,

I am not clear with the concept of usage of "Break" statements

Expand|Select|Wrap|Line Numbers
1. for(int r=0;r<9;r++)
2.    for(int c=0;c<9;c++)
3.    {
4.       if(current->brd[r][c]=='1')
5.       {
6.          cout<<"\nFound 1 @ (r,c)="<<r<<","<<c;
7.          if (up(current->brd, temp,r,c) )// the up mode is valid
8.          {
9.             //...
10.          }
11.          if(count)
12.          {
13.             break; // Will this break take the control out of the for loops.?
14.          }
15.       }
16.    }
17. }
I want to break out of the for loops whena condition is met. Will this work. It seems to me that this didnt work for me...

Thank You

Don
Feb 9 '07 #1
4 1827
horace1
1,510 Recognized Expert Top Contributor
The break command allows you to terminate and exit a loop (that is, do, for, and while) or switch command from any point other than the logical end. However, where switch and loop statements are nested, break exits the innermost one containing it.
so you can only break out of your inner loop (when count is non zero) - you can then test count again to break out of the outer loop - or you could make the outer for
Expand|Select|Wrap|Line Numbers
1.  for(int r=0;r<9 && (count == 0);r++)
2.  ..
3.
so when when count is non zero the outer for() exits
Feb 9 '07 #2
thefarmer
55 New Member
in some ways your command of placing your break statement is correct but, your if statement is lacking of idea how the break statement will terminate it,

you can try:

if ( count <=0)
{
break;
}

then this will work, but in your idea of using " if (count)" , it shows that the compiler doesn't know how will he herminate your command.

Hi,

I am not clear with the concept of usage of "Break" statements

Expand|Select|Wrap|Line Numbers
1.  for(int r=0;r<9;r++)
2.
3. for(int c=0;c<9;c++)
4.
5. {
6.
7. if(current->brd[r][c]=='1')
8.
9. {
10.
11. cout<<"\nFound 1 @ (r,c)="<<r<<","<<c;
12.
13. if (up(current->brd, temp,r,c) )// the up mode is valid
14.
15. {
16. ...
17. }
18.
19. if(count)
20. {
21. break; // Will this break take the control out of the for loops.?
22. }
23. }
24. }
25.
I want to break out of the for loops whena condition is met. Will this work. It seems to me that this didnt work for me...

Thank You

Don
Feb 9 '07 #3
Digital Don
19 New Member
Thank You man....I think I got the concept behind BREAK now clearly...

i.e. I can "break" out of only the inner most loop, not all the loops at a time...

and if i am using loop and switch and I use break inside switch statements, I will break out only of the switch statement but not the loop...right? The loop will still go on till loop condition !?
--------------------------------------------------------------------------------------
To "thefarmer" : The if(count) condition works fine...it takes (int)0 as 'false' so it works fine.

The break command allows you to terminate and exit a loop (that is, do, for, and while) or switch command from any point other than the logical end. However, where switch and loop statements are nested, break exits the innermost one containing it.
so you can only break out of your inner loop (when count is non zero) - you can then test count again to break out of the outer loop -

or you could make the outer for
Expand|Select|Wrap|Line Numbers
1. for(int r=0;r<9 && (count == 0);r++)
2. ..
3.
so when when count is non zero the outer for() exits
Feb 18 '07 #4
Ganon11
3,652 Recognized Expert Specialist
...and if i am using loop and switch and I use break inside switch statements, I will break out only of the switch statement but not the loop...right? The loop will still go on till loop condition !?
Yes. The break; statement breaks out of the current block of code, as long as it is a loop or a switch statement. So if you had a switch statement inside a loop, a break; statement there would indicate completion of the switch structure, and the loop would continue to execute normally.

If you're interested, there is also a continue; statement. If you want to stop the current execution of a loop without executing anything below a certain portion, but you still want to have the loop execute, use the continue; statement. For example, if you wrote a simple loop to perform operations on files named file1, file2, file3, you could write

Expand|Select|Wrap|Line Numbers
1. ifstream in;
2. for (int i = 1; i <= 5; i++) {
3.    // Make a cstring of the filename.
4.    in.open(myFileName);
5.    if (!in) {
6.       cout << Error opening " << myFileName << endl;
7.       continue;
8.    }
9.    // Do some stuff...
10. }
This way, if file1, file2, file3, and file5 exist - but file4 doesn't - when the program cannot find file4, it will stop trying to perform operations on the file, but it will continue with the rest of the loop so that the program accesses file5.
Feb 18 '07 #5