473,698 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31435
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...@NoMore PersonalMail.ne twrote:
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
107301
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"> <html> <head> <title>Form.html</title> </head>
30
2146
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 would be more orthogonal if a method could only have one return statement. --
4
1829
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
28042
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
8055
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 situation? Also I was wondering if it whould be wise to combine the standard status with return. exp:
3
3378
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 carriage return, I used "\r" thinking that when it was read back into the object it would be interpreted as a carriage return. Instead, I am getting the string literal instead of the escape sequence. Does anybody have a slick way of telling it...
1
1376
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 gallons; printf( "Enter the water consumption \n ", gallons ); scanf( "%f", &gallons);
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.