473,543 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

";" after if statement??

JS
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 ";".?
Jul 23 '05 #1
6 2260
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
Jul 23 '05 #2
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.
Jul 23 '05 #3
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.
Jul 23 '05 #4
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.
Jul 23 '05 #5
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.
Jul 23 '05 #6
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
Jul 23 '05 #7

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

Similar topics

23
3548
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:
68
4289
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.
27
3041
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. ...
0
10171
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...
10
2591
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...
5
8731
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...
2
46690
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...
2
2062
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
37
3897
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
10
1576
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...
0
7411
marktang
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...
0
7354
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...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7693
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...
0
5888
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...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1824
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
1
978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
643
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...

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.