LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-29-2012, 12:23 PM   #1
ed88
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

Code:
while(true)
{
......;
.....;
if (exitKey == 1)
{
break;
}
}
 
Old 11-29-2012, 01:45 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
A more common method would be
Code:
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.

Markus

Last edited by markush; 11-29-2012 at 01:47 PM.
 
Old 11-29-2012, 03:27 PM   #3
linosaurusroot
Member
 
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, 04:46 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,388

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
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:
Code:
// repeated code, bad
key = getkey();
exitKey = (key == EXIT);
while (exitKey != 1) {
    ...
    key = getkey();
    exitKey = (key == EXIT);
}
Code:
// super complicated unreadable loop condition, bad
while ((exitKey = ((key = getkey()) == EXIT)) != 1) {
    ...
}
Code:
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, 05:28 PM   #5
theNbomr
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.
 
  


Reply


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 04:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 06:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 09:47 PM
Program on boot-up won't stop running in a continuous loop... CanadianSlacker Slackware 6 01-14-2004 02:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

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