473,320 Members | 2,145 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,320 software developers and data experts.

difference between break and return

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?
Nov 15 '05 #1
5 40156
Return takes you out of the function, Break gets you out of the loop
"tony collier" <me*****@hotmail.com> wrote in message
news:Xn*******************************@140.99.99.1 30...
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?

Nov 15 '05 #2
True purists will say that a function should only have a single entry (at
the top) and exit (at the bottom) point.

Really, it gets down to whether or not you want to continue executing your
code after a certain loop condition has happened. Those that use returns
within a loop (and I am guilty of the occasional usage myself) simply want a
quick way to exit the function without writing additional logic to support
the "single exit" philosophy.

Remember, they do have different purposes as BREAK will only exit the loop
that you are in but execution of the function continues.
"tony collier" <me*****@hotmail.com> wrote in message
news:Xn*******************************@140.99.99.1 30...
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?

Nov 15 '05 #3


thankyou. don't chastise yourself too harshly - the example that got me
thinking was microsoft coded.
Nov 15 '05 #4
G'Day Tony,

The major difference between break and return is that with return you exit
the method whereas with break not necessarily. I suggest the use of break in
loops rather than return.
Nov 15 '05 #5
In article <10***********@pluto.global.net.au>, "Eki Y. Baskoro"
<ek*********@sonet.net.au> wrote:
The major difference between break and return is that with return you exit
the method whereas with break not necessarily. I suggest the use of break in
loops rather than return.


You could consider "break" to be like that most hated of programming
language commands, "goto". It causes your program to immediately go to the
right-hand side of the closing brace of the closest loop.

F'rinstance:

int a[100];
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
a[i*10+j]=1;
if (i==j) break;
a[i*10+j]=1;
} // <-- The 'break' jumps to just after this brace,
// and before any statements that follow it.
} // <-- The 'break' does not jump to this brace,
// because there's a closer brace that terminates a loop.

Note that this rather clumsily creates something like the lower half of an
identity matrix in the array a. If we'd used a "return", then we would only
have set _one_ element - a[0] - to 1, and then exit the function. To create
the whole identity matrix, we'd use "continue" instead of "break" -
"continue" jumps to the _left_ hand side of the nearest loop-ending brace.

Essentially, "continue" means "stop processing this leg of this loop, and go
round again." "break" means "stop processing this loop". "return" means
"quit the subroutine / function / method that we're in" - it'll skip out of
any number of enclosing loops.

Alun.
~~~~

[Please don't email posters, if a Usenet response is appropriate.]
--
Texas Imperial Software | Find us at http://www.wftpd.com or email
1602 Harvest Moon Place | al**@texis.com.
Cedar Park TX 78613-1419 | WFTPD, WFTPD Pro are Windows FTP servers.
Fax/Voice +1(512)258-9858 | Try our NEW client software, WFTPD Explorer.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: w i l l | last post by:
Why does this work the way it does? If someone could explain return true, and return false to me I'd greatly appreciate it. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
30
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
4
by: Ganesh Gella | last post by:
Hi All, At some places in code, I see following statements, const XMLCh* some_func() { return &*(*i)->m_Value.begin(); } Is that any different from following syntax ?
5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
3
by: Dinsdale | last post by:
I have an xml file that is read into an object using serialization. One of the objects has a string field called delimeter that I want to contain a carriage return. Instead of trying to include the...
1
by: m konstantakis | last post by:
#include <stdio.h> int main(void) { int currentprice,upcomingprice; int currentprice1,currentprice2,currentprice3; int upcomingprice1,upcomingprice2,upcomingprice3; int difference; float...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.