473,888 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not clear on the use of break statements

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

Please Help...

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

Please Help...

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

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

Similar topics

19
105857
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: System.out.print((char)27 + "[2J");
8
34038
by: TaiwanNoWhere | last post by:
// case 1 int Option = 0; for(;;) { cin >> Option; switch(Option) { case 1:
8
380
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. #include <iostream> #include <iomanip> using namespace std;
5
40272
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
3751
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 that can make code faster than fast. thank you.
1
1476
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 OR SOMETHING SIMILAR. Will VB.NET has a break statement in the coming Whidbey release?
6
1733
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
31450
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 condition is met, should I use break or return to stop the loop? e.g. is something like this ok? Would using return do the same thing? var dow = ;
11
9725
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
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11186
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.