| | |
My first C++ problem :(
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have just started using C++ for the first time and I love it!!!
First problem I have encoutered proably a simple one but
Now the idea is if b is equal to 5 then it breaks out of the if loop, the for loop c, the for loop b, the for loop a.
Am I doing it right?
If not can someoone please correct me?
Thankyou, Regards X
PS: While on the topic what happens if I only wanted to break out of the if loop or if loop + for loop a or if loop + for loop a + for loop b. Thanks Again.
First problem I have encoutered proably a simple one but
C++ Syntax (Toggle Plain Text)
for (int a=0; a<10; a++) { for (int b=0; b<10; b++) { for (int c=0; c<10; c++) { if (b == 5) { break; beak; break; break; } } } }
Now the idea is if b is equal to 5 then it breaks out of the if loop, the for loop c, the for loop b, the for loop a.
Am I doing it right?
If not can someoone please correct me?
Thankyou, Regards X
PS: While on the topic what happens if I only wanted to break out of the if loop or if loop + for loop a or if loop + for loop a + for loop b. Thanks Again.
Last edited by OmniX; Jul 17th, 2008 at 11:15 am.
"You never stop learning." - OmniX
>Now the idea is if b is equal to 5 then it breaks out of
>the if loop, the for loop c, the for loop b, the for loop a.
Nope. You can write as many breaks as you want, but only the first one will have any effect, and it'll break you out of the inner-most loop (the c loop).
>If not can someoone please correct me?
You have options here, but the best option is not to get into such a situation to begin with. If you need to break out of a nested loop, it's usually best (if you can manage it) to refactor the nested loops into a function and then use the return value to break:
>the if loop, the for loop c, the for loop b, the for loop a.
Nope. You can write as many breaks as you want, but only the first one will have any effect, and it'll break you out of the inner-most loop (the c loop).
>If not can someoone please correct me?
You have options here, but the best option is not to get into such a situation to begin with. If you need to break out of a nested loop, it's usually best (if you can manage it) to refactor the nested loops into a function and then use the return value to break:
C++ Syntax (Toggle Plain Text)
for ( int a = 0; a < 10; a++ ) { if ( !some_function() ) break; } ... bool some_function() { for ( int b = 0; b < 10; b++ ) { for ( int c = 0; c < 10; c++ ) { if ( b == 5 ) return false; } } return true; }
I'm here to prove you wrong.
That would be a good idea use of functions but I am not able to in this problem 
If i set it to a bool function then return false it will break me out completly, correct?
I require to just break out for loops a, b, c when required what would I do in that case?
Eg.
Break out of for loop a:
Break out of for loop a, b:
Break out of for loop a, b, c:
Thanks, Regards X

If i set it to a bool function then return false it will break me out completly, correct?
I require to just break out for loops a, b, c when required what would I do in that case?
Eg.
Break out of for loop a:
Break out of for loop a, b:
Break out of for loop a, b, c:
Thanks, Regards X
"You never stop learning." - OmniX
Then use
goto C++ Syntax (Toggle Plain Text)
for (int a=0; a<10; a++) { for (int b=0; b<10; b++) { for (int c=0; c<10; c++) { if (b == 5) goto endloop; } } } endloop:
Last edited by invisal; Jul 17th, 2008 at 1:07 pm.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
Thanks!!!
Very nice, so I can have multiple goto statements also?
a;
b;
c;
goto a;
goto b;
goto c;
Something like that?
Very nice, so I can have multiple goto statements also?
a;
b;
c;
goto a;
goto b;
goto c;
Something like that?
"You never stop learning." - OmniX
Of course you can have multiple of them. However, try to avoid using it as much as possible since most of the time
goto can be replaced with a loop. For some case such as break multiple-level of loops, goto is inevitable. Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
>For some case such as break multiple-level of loops, goto is inevitable.
I wouldn't say inevitable, but goto can be the cleanest solution if you have deep nesting and can't refactor it. The alternative is to use flags and check them at each level, which is a "pure" structured solution, but it's almost always ugly as sin.
I wouldn't say inevitable, but goto can be the cleanest solution if you have deep nesting and can't refactor it. The alternative is to use flags and check them at each level, which is a "pure" structured solution, but it's almost always ugly as sin.
I'm here to prove you wrong.
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: do while loop
- Next Thread: Creating charts in C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






