In this sample code:
if(n==0&&args>1 ){
for(i=num;args> i+1;i++){
s[i].length = 0;
opt = document.create Element('OPTION ');
s[i].appendChild(op t);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};
the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".? 6 2267
JS wrote: In this sample code:
if(n==0&&args>1 ){ for(i=num;args> i+1;i++){ s[i].length = 0; opt = document.create Element('OPTION '); s[i].appendChild(op t); opt.value = ""; opt.text = "\74-- Vælg --"; } return true };
the if statement is ended by a ";" but I have seen other if statments that do not end with that. Why is if statement sometimes ended by a ";".?
I guess, the semicolon should be after "return true" and not after the
closing bracket.
best regards
Deniz
JS wrote: In this sample code:
if(n==0&&args>1 ){ for(i=num;args> i+1;i++){ s[i].length = 0; opt = document.create Element('OPTION '); s[i].appendChild(op t); opt.value = ""; opt.text = "\74-- Vælg --"; } return true };
the if statement is ended by a ";"
No it isn't. The semicolon, following the closing brace that represents
the end of the Block statement that is the conditionally executed
statement of the IfStatement, is an EmptyStatment. Harmless but
unnecessary.
but I have seen other if statments that do not end with that.
The IfStatement production does not require a terminating semicolon. If
the Statement ending the production - if ( Expression) Statement - is
not a Block statement then that statement may need to be terminated with
a semicolon, giving the impression that the IfStatment ends with a
semicolon.
ECMAScript performs 'automatic semicolon insertion' so under some
circumstances semicolons that are required by the production rules may
be omitted as the interpreter will insert them itself. See ECMA 262 3rd
edition section 7.9.
Why is if statement sometimes ended by a ";".?
The existence of the EmptyStatment - ; - (which is harmless in most
contexts) and automatic semicolon insertion allows script authors to get
away with having little understanding of when and where semicolons are
necessary in javascript. That lack of understanding can be observed when
looking at example scripts.
Richard.
On 27/05/2005 08:56, Richard Cornford wrote:
[snip] The existence of the EmptyStatment - ; - (which is harmless in most contexts) and automatic semicolon insertion allows script authors to get away with having little understanding of when and where semicolons are necessary in javascript. [...]
I'd disagree with the former, and agree with the latter.
Empty statements are a part of other languages, including compiled ones,
which have no qualms over enforcing strict adherence to the grammar. I
can't think of enough examples to decide if empty statements are ever
necessary, but they can be used, for example, in iterative statements
where the condition also performs an action that renders the loop body
redundant:
while( condition );
However, this could be equally represented with:
while( condition ) {}
as the StatementList within a Block is optional. This is true in C (and
C++), and Java (the grammar summary for the latter is misprinted).
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Michael Winter wrote: Richard Cornford wrote:
<snip> The existence of the EmptyStatment - ; - (which is harmless in most contexts) and automatic semicolon insertion allows script authors to get away with having little understanding of when and where semicolons are necessary in javascript. [...]
I'd disagree with the former, and agree with the latter.
Empty statements are a part of other languages, including compiled ones, which have no qualms over enforcing strict adherence to the grammar.
<snip> while( condition );
<snip>
I have nothing against empty statments, and would prefer using one in an
IterationStatem ent where all the work was done in the control expression
(rather than an empty block).
But having empty statements allows them to be inserted where semicolons
would not otherwise be necessary (you would get some sort of error if
they did not exist), and that is true of compiled languages as well,
except in compiled languages you don't get away with omitting the
necessary semicolons so you end up learning where they need to be very
quickly.
The OP's question relates to an unnecessary EmptyStatement rather than
the missing semicolon at the end of the ReturnStatement (which is fixed
by automatic semicolon insertion). An example, seeing:-
var x = function(){
... // function body
};
- which is correct because the assignment expression becomes an
ExpressionState ment and should be semicolon terminated, someone might
infer:-
function x(){
... // function body
};
- where the semicolon is an EmptyStatement independent of the function
declaration. But getting away with that may serve to blur the
distinction between a function declaration and a function expression as
a side effect.
Richard.
Richard Cornford wrote:
<snip> var x = function(){ ... // function body };
- which is correct because the assignment expression becomes an ExpressionState ment and should be semicolon terminated, ...
<snip>
Actually the assignment expression becomes part of a VariableStateme nt,
which is also semicolon terminated. I probably should have left the -
var - keyword out.
Richard.
In article <d7**********@n ews.net.uni-c.dk>, JS <ds***@asdf.com .invalid>
writes In this sample code:
if(n==0&&args>1 ){ for(i=num;args> i+1;i++){ s[i].length = 0; opt = document.create Element('OPTION '); s[i].appendChild(op t); opt.value = ""; opt.text = "\74-- Vælg --"; } return true };
the if statement is ended by a ";" but I have seen other if statments that do not end with that. Why is if statement sometimes ended by a ";".?
It might help to understand other people's answers if the code is laid
out to show how the javascript parser will see it :
if (n==0 && args>1)
{
for (i=num; args>i+1; i++)
{
s[i].length = 0;
opt = document.create Element('OPTION ');
s[i].appendChild(op t);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
}
;
Each extra level of indenting marks a statement or statements nested
inside another statement. (The parser will also automatically add a
semicolon after 'true').
John
--
John Harris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Invalid User |
last post by:
While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:
if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"
which was supposed to be:
|
by: Marco Bubke |
last post by:
Hi
I have read some mail on the dev mailing list about PEP 318 and find the new
Syntax really ugly.
def foo(x, y): pass
I call this foo(1, 2), this isn't really intuitive to me!
Also I don't like the brackets.
|
by: Ron Adam |
last post by:
There seems to be a fair amount of discussion concerning flow control
enhancements lately. with, do and dowhile, case, etc... So here's my
flow control suggestion. ;-)
It occurred to me (a few weeks ago while trying to find the best way to
form a if-elif-else block, that on a very general level, an 'also'
statement might be useful. So I was wondering what others would think
of it.
|
by: Jan |
last post by:
I store sql-commands in a database table. In the first step I get the
sql command out of the database table with embedded sql. In the second
step I try to execute the command, which i got from the database
table, using dynamic sql.
Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the
error code -2149 is returned That means "Specified partition does not
exist".
Does anybody know if it is a database problem or a problem of
|
by: LaEisem |
last post by:
On-the-job, I have "inherited" a lot of old C language software.
A question or two about when "casting" of null pointer constants
is needed has occurred during behind-the-scenes cleanup
of some of that software. That subject seems not to be addressed,
at least not directly, in the C FAQ where FAQ 5.2 seems most relevant.
References:
* C FAQ 5.2 Null pointers
(Including conditions where "casting" of
null pointer...
| |
by: charliewest |
last post by:
I've implemented the USING statement to ensure that my newly created sql
connection closes when my method is finished using it. The USING statement is
wrapped in try/catch error handling statement. This works fine.
When i try to implement the "SqlTransation" class, the code does not work as
the SqlTransation object is out-of-scope in the catch statement, and/or,
after the USING statement ends, the sql connection object is automatically...
|
by: Richard |
last post by:
Our web programmer was looking in his application log an found the
following error:
2006-08-31 16:33:35,129 ERROR org.hibernate.util.JDBCExceptionReporter
- < SQL0723N An error occurred in a
triggered SQL statement in trigger "OLGCWEB.TBLPROPS_INS_TRG".
Information returned for the error includes SQLCODE "-811", SQLSTATE
"21000" and message tokens "". SQLSTATE=09000
2006-08-31 16:33:35,139 ERROR...
|
by: marsarden |
last post by:
write code like:
int main(void)
{
int a=10;
if(a<20)
{}
}
Compiler ok on dev-cpp . don't we have to add a ";" after if
|
by: jht5945 |
last post by:
For example I wrote a function:
function Func()
{
// do something
}
we can call it like:
var obj = new Func(); // call it as a constructor
or
var result = Func(); // call it as a function
|
by: Prisoner at War |
last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth
the Py3K folks want to mess with a simple thing like the "print"
"command" (is that what it's called, a command?), turning it into
"print()"...I mean, what's the point, exactly?? To look like a more
"traditional" computer-language format?
And what's with not supporting the so-called softspace "feature" of
the current "print" command, where a space after a comma, like
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |