473,385 Members | 1,326 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,385 software developers and data experts.

Difference between break and return?

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 = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday'];

var d = 'Wednesday';

for(var i = 0; i < dow.length; i++){

if (dow[i] == d){

var abc = dow[i];

break;

}

}

Sep 11 '07 #1
3 31396
dn
Yansky wrote:
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.
simply put,

break exits a loop,
return exits a function.

D.

Sep 11 '07 #2
On Sep 12, 3:05 am, dn <dana...@NoMorePersonalMail.netwrote:
Yansky wrote:
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.

simply put,

break exits a loop,
return exits a function.

D.
Thanks. :)

Sep 11 '07 #3
dn wrote:
Yansky wrote:
>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.

simply put,

break exits a loop,
return exits a function.
That is too much simplified.

For the `break' statement may also occur outside of loops (i.e. `for',
for..in, do..while, and while..do statements followed by an execution
block), in the `switch' statement where it prevents execution from
considering the following `case' statements or the following `default'
statement within that `switch' statement.

Also, `break' does not return a value. `return', which exits the current
local execution context (the current function), does (if not explicitly
stated, it returns `undefined' from that context implicitly).
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 11 '07 #4

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: 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.