Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-29-2012, 11:23 AM   #1
LQ Newbie
Registered: Apr 2008
Posts: 13

Rep: Reputation: 0
use of brack to stop a loop

Hi to all. I have a question about using in C++ the comand break to stop while or for loop my teacher says thats is a rong method to do this after this i was searching on internet about this and i have found many places that use break to stop it 1 of this pages was microsoft..... im not sure if this is a bad programing method or my teacher is rong what do you opine about this and if I'm rong pls tell me how to do it.

ex. of my code

if (exitKey == 1)
Old 11-29-2012, 12:45 PM   #2
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
A more common method would be
while(exitKey != 1)

It is better programmingstyle because one sees at the beginning of the loop what's the exit condition. You should not forget that not only you read the code you write and therefore should make it as readable as possible.


Last edited by markush; 11-29-2012 at 12:47 PM.
Old 11-29-2012, 02:27 PM   #3
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
The issue might not only be whether you see the termination condition at the beginning but whether there is more than one way to leave the code block. If the teacher is trying to encourage Hoare logic for reasoning about code one exit point is often preferred (but I wouldn't make an absolute rule of it depending on the details).
Old 11-29-2012, 03:46 PM   #4
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,494

Rep: Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789
Originally Posted by markush View Post
A more common method would be...
It is better programmingstyle because one sees at the beginning of the loop what's the exit condition.
The problem with this style is that you either need to repeat the reading of the input, or stuff the read function into the condition:
// repeated code, bad
key = getkey();
exitKey = (key == EXIT);
while (exitKey != 1) {
    key = getkey();
    exitKey = (key == EXIT);
// super complicated unreadable loop condition, bad
while ((exitKey = ((key = getkey()) == EXIT)) != 1) {
for (;;) { // I tend to use for (;;) instead of while (true)
    key = getKey();
    exitKey = (key == EXIT);

    // You can use whitespace to make the exit condition standout.
    // I've also seen suggestions to outdent, though I don't do
    // that because that kind of indentation is hard to do automatically.

  if (exitKey) break;

This pattern is called Loop and a Half.
1 members found this post helpful.
Old 11-29-2012, 04:28 PM   #5
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is substantially a matter of style (and style does count), sort of like the use of punctuation and capitalization in prose.
--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
Program on boot-up won't stop running in a continuous loop... CanadianSlacker Slackware 6 01-14-2004 01:06 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration