473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Not clear on the use of break statements

Digital Don
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...

Please Help...

Thank You

Don
Feb 9 '07 #1
4 1810
horace1
1,510 Expert 1GB
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
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...

Please Help...

Thank You

Don
Feb 9 '07 #3
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 Expert 2GB
...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

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
8
by: TaiwanNoWhere | last post by:
// case 1 int Option = 0; for(;;) { cin >> Option; switch(Option) { case 1:
8
by: Richard | last post by:
The program I write ask two questions. If user enter of the question invalid or out of range, then the program will exit. My code cannot do that with a break. Can anyone take a look and help. ...
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
55
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things...
1
by: Neo | last post by:
I am in dire need of a break statement in VB.NET language. I have several pieces of code that would have much cleaner look and much less deeper if/else/endif nests IF VB.NET HAS A BREAK STATEMENT...
6
by: David | last post by:
I know that by some reasons... the use of "break;" in java language is not correct, is there any similar problems with c#????
3
by: Yansky | last post by:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still uncertain as to the difference between using break and using return. If I have a simple "for" loop that I want to stop if a...
11
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.